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'));
精彩评论