在jQuery中向上遍历9个级别
在A href标记上发生了一个单击事件,我需要引用A的9级以上的类 因此:在jQuery中向上遍历9个级别,jquery,traversal,Jquery,Traversal,在A href标记上发生了一个单击事件,我需要引用A的9级以上的类 因此: 我知道的唯一方法是调用parent()9次,我还能做什么?您可以通过parents函数提供选择器 $(a).click(function() { var theIdIs = $(this).parents("div.blah").attr("id"); }); 另外,如果您使用jQuery 1.3+,也可以使用该方法 您可以执行div.blah:first或.slice(0,1)来获取最接近的值。(例如,如果
我知道的唯一方法是调用parent()9次,我还能做什么?您可以通过parents函数提供选择器
$(a).click(function() {
var theIdIs = $(this).parents("div.blah").attr("id");
});
另外,如果您使用jQuery 1.3+,也可以使用该方法
您可以执行div.blah:first或.slice(0,1)来获取最接近的值。(例如,如果你想要最近的div,而外面有更多div。)哦,不知道slice,很好!
$(a).click(function() {
var theIdIs = $(this).parents("div.blah").attr("id");
});
$('a').click(function() {
if ($(this).closest("div.blah").hasClass("blah"))
{
// Do something with it
}
});