开发者

How to get the class name of a preceding div element with jquery?

开发者 https://www.devze.com 2023-03-14 04:44 出处:网络
I am developing a new design for which I need to get the name of the class of a div element above another div element.

I am developing a new design for which I need to get the name of the class of a div element above another div element.

Like this:

<div class="random name a"></div>
<div class="the name of this class depends on the 开发者_运维问答name of the class above this div"></div>

Is there any way of knowing this with jQuery? I been looking around but couldn't find any clue yet.

Thanks a lot!


You can use .prev for this - http://api.jquery.com/prev/

$('div.the').prev().attr('class');


Grab the class from the previous element:

$(this).prev().attr("class");


$(this).addClass($(this).prev('div').attr('class'));


<div class="div1"></div>
<div class="div2"></div>

var prevDivClass = $('.div2').prev().attr('class'));
0

精彩评论

暂无评论...
验证码 换一张
取 消