2012-11-26 292 views
1

我試圖隱藏「箭頭」點擊「thead」但它不工作。 jquerys的「孩子」不能在桌子上工作嗎?點擊隱藏子元素

HTML

<thead class="thead"> 
    <tr> 
    <th> 
     <div class="arrow_o"></div> 
    </th> 
    </tr> 
</thead> 

JS

$(document).ready(function() { 
    $('.thead').click(function() { 
    $(this).children('.arrow_o').hide(); 
    }); 
}); 
+0

如果您在使用特定的jQuery方法時遇到問題,那麼最好的地方就是[該方法的doco](http://api.jquery.com/children/)... – nnnnnn

回答

5

的。兒童()方法從.find不同()在。兒童()只 行進的單一電平向下DOM樹而.find( )可以遍歷 多層次選擇後代元素(孫, 等)以及。

因此,這:

$(document).ready(function() { 
    $('.thead').click(function() { 
    $(this).find('.arrow_o').hide(); 
    }); 
}); 
+0

非常感謝您:) – user1766306

+0

此外,檢查出Vega的答案,$(selector,this)是一個優雅的,如果一個不太可讀的方式來獲取子元素。 – Kato

+0

@Kato設置jQuery中的上下文本質上是在jQuery中內部翻譯爲在特定上下文中使用'.find()'方法,所以這仍然是最好的解決方案,因爲它更具可讀性:) – Bruno

2

嘗試$('.arrow_o', this).hide();基本上設置在arrow_o應位於上下文。

全碼:

$(document).ready(function() { 
    $('.thead').click(function() { 
    $('.arrow_o', this).hide(); 
    }); 
}); 
2

的。兒童()方法從.find()在。兒童(不同)僅行進單個電平向下DOM樹而.find()可以遍歷多個層次來選擇後代元素(孫輩等)。

jQuery children

使用.find()代替。