假设我有这个 HTML:
<div class="SomeParent" id="BlueParent">
<div>
<div class="Test">test</div>
<div>
</div>
Test
类的 div 实际上深深嵌套在 SomeParent
容器中。获取 Test 是其后代的 SomeParent
类的 id 的最快方法是什么。 .parent()
是最好的方法吗?
感谢您的建议。
最佳答案
也许
$('.Test').closest('.SomeParent').attr('id');
http://api.jquery.com/closest/
但是,我不知道它是否会比 thecodeparadox 的解决方案更快或更慢。
关于某个类的远程父级的 jquery id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11888367/