2

在href标签上发生点击事件,我需要引用9级以上的a类,

所以:

<div class="blah" id="234">
 <div>
     <div>
        <div>
          <div>
            <div>
             <div><a href=""></a>

我知道的唯一方法是调用 parent() 9 次,我还能做什么?

4

2 回答 2

7

您可以通过父母功能提供选择器

$(a).click(function() {
   var theIdIs = $(this).parents("div.blah").attr("id");
});
于 2009-02-21T13:01:20.497 回答
4

此外,如果您使用 jQuery 1.3+,您可以使用最接近的方法

$('a').click(function() {
   if ($(this).closest("div.blah").hasClass("blah"))
   {
      // Do something with it
   }
});
于 2009-02-21T15:49:21.920 回答