如何使用jquery获取前面div元素的类名?

时间:2011-06-17 19:25:51

标签: jquery html css

我正在开发一个新设计,我需要在另一个div元素之上获取div元素的类名。

像这样:

<div class="random name a"></div>
<div class="the name of this class depends on the name of the class above this div"></div>

有没有办法用jQuery知道这个?我一直在四处寻找,但却找不到任何线索。

非常感谢!

4 个答案:

答案 0 :(得分:4)

你可以使用.prev - http://api.jquery.com/prev/

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

答案 1 :(得分:0)

从上一个元素中抓取类:

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

var prevDivClass = $('.div2').prev().attr('class'));
相关问题