在href标签上发生点击事件,我需要引用9级以上的a类,
所以:
<div class="blah" id="234">
<div>
<div>
<div>
<div>
<div>
<div><a href=""></a>
我知道的唯一方法是调用 parent() 9 次,我还能做什么?
您可以通过父母功能提供选择器
$(a).click(function() {
var theIdIs = $(this).parents("div.blah").attr("id");
});
此外,如果您使用 jQuery 1.3+,您可以使用最接近的方法
$('a').click(function() {
if ($(this).closest("div.blah").hasClass("blah"))
{
// Do something with it
}
});