Javascript jQuery找到最高的父TD
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8159814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery find highest parent TD
提问by Zak
I'm working on a code for a form contained within a table. I'm writing (with jQuery) a function to highlight the parent <td>of each <input>element. That part is simple - the code is just:
我正在为表格中包含的表单编写代码。我正在编写(使用 jQuery)一个函数来突出显示<td>每个<input>元素的父元素。那部分很简单 - 代码只是:
$('.myForm input').click(function(){
$(this).parent().addClass('active');
})
The more complicated part is that some text fields are inside of a second table nested within a <td>of the first table. It would look like:
更复杂的部分是某些文本字段位于嵌套在<td>第一个表的a内的第二个表中。它看起来像:
<table>
<tr>
<td> <--cell I want to add the class to
<table>
<tr>
<td><input type='text'></td>
</tr>
</table>
</td>
</tr>
</table>
So my question is this: is there a way to use one jQuery statement to find the highest parent <td>of the <input>element? So in other words, can I combine:
所以我的问题是:有没有办法使用jQuery的一个语句找出最高父方式<td>的的<input>元素吗?换句话说,我可以结合:
$('.myForm input').click(function(){
$(this).parent().addClass('active');
})
and
和
$('.myForm input').click(function(){
$(this).parent().parent().addClass('active');
})
into one function?
成为一个功能?
回答by lonesomeday
The best solution is to add a class to the table you actually want to target. This means that you could update the markup in future without necessarily breaking the JS, by doing something like $(this).closest('.targetElement').addClass('active').
最好的解决方案是将一个类添加到您实际想要定位的表中。这意味着您可以在将来更新标记而不必破坏 JS,通过执行类似$(this).closest('.targetElement').addClass('active').
If you can'tdo that, you can use parents('td').last(). This selects all tdparent elements and then gets the last one.
如果你不能这样做,你可以使用parents('td').last(). 这将选择所有td父元素,然后获取最后一个。
$('.myForm input').click(function(){
$(this).parents('td').last().addClass('active');
})
See the jQuery manual:
请参阅 jQuery 手册:
回答by Rocket Hazmat
Try doing this:
尝试这样做:
$('.myForm input').click(function(){
$(this).parents('td').last().addClass('active');
})
回答by lsuarez
I'd suggest trying:
我建议尝试:
$(this).parents("td").last()
It will find all table cell ancestors of the current element. The last one should contain the highest level table cell element.
它将找到当前元素的所有表格单元格祖先。最后一个应该包含最高级别的表格单元格元素。
回答by Michal Kuklis
you can try:
你可以试试:
$(this).parents('td:last');
or
或者
$(this).parents('td').last();
回答by BumbleB2na
Give your top-level td element a class name:
给你的顶级 td 元素一个类名:
<table>
<tr>
<td class="topTD"> <--cell I want to add the class to
<table>
<tr>
<td><input type='text'></td>
</tr>
</table>
</td>
</tr>
</table>
$('.myForm input').click(function(){
$(this).closest('td.topTD').addClass('active');
});
回答by Cyrill
Quick&dirty :)
又快又脏:)
$(this).parents('td')[--$(this).parents('td').length]

