[英]How to get child class of parent id using jquery?
我只想問,如何獲得子元素的類。 例如,如果我有這種代碼:
<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示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.