Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JQuery:如何选择父表进行解析_Javascript_Jquery - Fatal编程技术网

Javascript JQuery:如何选择父表进行解析

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) {

我有一个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) {
        $(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树中有多个表的情况下获取最顶层的表,而as
closest
则获取您实际要处理的表。

我知道这个答案已经很晚了,然而,我在谷歌的一些搜索中发现了这个答案,所以我认为这是值得贡献的。