gpt4 book ai didi

jQuery:如果所选元素 $(this) 的父元素的类名为 'last'

转载 作者:行者123 更新时间:2023-12-03 22:23:32 24 4
gpt4 key购买 nike

我一定错过了一些非常重要的东西,我一直在使用 .parent().parent().parent().. 等来遍历 DOM 和 .next().next() 来遍历 DOM。

我知道这是错误的,我需要更可靠的东西,我需要一个选择器,它将从单击的元素 $(this) 向下遍历 DOM,以查看单击的元素是否位于具有“last”类的元素内”。

div.last > div > table > tr > td > a[THE ITEM CLICKED is in the element last]

div > div > table > tr > td > a[THE ITEM CLICKED is not in the element last]

如果结果有长度

var isWithinLastRow = [amazingSelector].length;

在这种情况下做其他事情。

最佳答案

试试这个:- http://jsfiddle.net/adiioo7/PjSV7/

HTML:-

<div class="last">
<div>
<table>
<tr>
<td>
<a>Test</a>
</td>
</tr>
</table>
</div>
</div>
<div>
<div>
<table>
<tr>
<td>
<a>Test</a>
</td>
</tr>
</table>
</div>
</div>

JS:-

jQuery(function($){
$("a").on("click",function(){
if($(this).closest(".last").length>0)
{
alert("Clicked anchor with div parent class as last");
}
});
});

关于jQuery:如果所选元素 $(this) 的父元素的类名为 'last',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17068838/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com