2012-04-13 63 views
-4

正如标题所示,我想通过单击另一个div(“div.secondclass”)来获得动态div的类。这里是我的代码:jQuery - 通过点击另一个div来获得动态div的类

$(document).ready(function() { 
    $("div.secondclass").click(function() { 
     firstclass=$('#firstid').attr('class'); 
     alert("the class of the 1st div is ="+firstclass); 
    }); 
}); 
+1

是如何彼此相关的两个div? – Gary 2012-04-13 06:37:01

+2

恩,firstid是id。如果你不知道它,你可以用attr('id') – 2012-04-13 06:37:10

+0

得到它有这个问题吗? – steveukx 2012-04-13 06:37:42

回答

0
$(document).ready(function() { 
    $("div.secondclass").click(function() { 
     var aClass = $('#firstid')[0] 
          .className 
          .replace(/[\n\t\r]/g, " ") 
          .replace(/^\s+|\s+$/g, "") 
          .split(" "); 
     alert("The div with the id firstid has got these classes: " + aClass.join(", ")); 
    }); 
}); 
相关问题