如何使用jquery获取父ID的子类

How to get child class of parent id using jquery?

本文关键字:ID 子类 获取 何使用 jquery      更新时间:2023-09-26

我只想问,如何获取子元素的类。例如,如果我有这种代码:

<a class="w010 h010 p007 b03 d2 f1" id="category_entertainment">
    <div class="it3 ib3 il3 jt05 jb05 jl10 kt04 kb04 kl03">
        //some codes here. . . 
    </div>
</a>

现在我有一个锚标签。我的方案是在单击标签后。我需要获取div 的类并将其替换为我的新类。

$("#category_entertainment").on('click',function(){
    var new_class = "it3 ir3 il3 jt10 jr05 jl05 kt03 kr04 kl04";
    $("#category_entertainment > div).attr("class",new_class); //i tried this but not working.
});

这在我的机器上正常工作:

$("#category_entertainment").on('click',function(){
    var new_class = "it3 ir3 il3 jt10 jr05 jl05 kt03 kr04 kl04";
    $("#category_entertainment div").attr("class",new_class);
});

我唯一的建议是确保此代码住所在创建元素后运行。 如果您将其放在window.onload=function() {// assign click handler}类似的东西中以确保加载元素。 下面是一个 jsfiddle 示例。

JSFIDDLE 示例