jQuery - 通过单击另一个div获取动态div的类

时间:2012-04-13 06:31:49

标签: javascript jquery html

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

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

1 个答案:

答案 0 :(得分: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(", "));
    });
});