Javascript JQuery:如何选择父表进行解析
我有一个JS函数,它通过一个表进行解析:Javascript JQuery:如何选择父表进行解析,javascript,jquery,Javascript,Jquery,我有一个JS函数,它通过一个表进行解析: // id contains a message id, activeRow is "this" from onClick on tr function doSomething ( id, activeRow ) { // AJAX calling using id as parameter $("#searchResultTable > tbody > tr").each(function(index) {
// id contains a message id, activeRow is "this" from onClick on tr
function doSomething ( id, activeRow ) {
// AJAX calling using id as parameter
$("#searchResultTable > tbody > tr").each(function(index) {
$(this).removeClass("bold");
});
}
这非常好(多亏了Ariel@),但我认为应该有另一种可能性,比如:
var table = $(activeRow).parent();
$("tbody > tr", table).each(function(index) {
// .. do something
});
// The above clearly doesn't work, but should document what I'm looking for.
这将允许使用相同的表ID,而函数将分别处理它们中的每一个
非常非常感谢 Close,您可能希望使用表上的类
$("table.className tbody tr").each(function() {
// .. do something
});
jQuery的方法使获取父表成为一个快照:
$(activeRow).parents('table')[0];
那么:
$(activeRow).siblings().andSelf().removeClass("bold")
这将在
activeRow
中获取
,也抓取其同级
,并从所有类中删除“bold”
类。最好使用最近的
,如下所示:
$(activeRow).closest('table')[0];
从这里开始:
最近:沿DOM树向上移动,直到找到所提供选择器的匹配项
父元素:沿DOM树向上移动到文档的根元素,将每个祖先元素添加到临时集合中;然后,如果提供了选择器,它将根据选择器筛选该集合
在这种情况下,
parents
很可能会在DOM树中有多个表的情况下获取最顶层的表,而asclosest
则获取您实际要处理的表。我知道这个答案已经很晚了,然而,我在谷歌的一些搜索中发现了这个答案,所以我认为这是值得贡献的。