代码之家  ›  专栏  ›  技术社区  ›  Atif

jquery filter()遍历似乎不起作用?

  •  1
  • Atif  · 技术社区  · 16 年前

    我不知道这有什么问题?

    $('.post').live('mouseenter mouseleave', function() {  
             $(this).filter('anything here,a,div,.class,#id').toggleClass('hidden');
        });
    

    如果这样做的话。

    $('.post').live('mouseenter mouseleave', function() {  
             $(this).toggleClass('hidden');
        });
    

    有一个锚,我想在鼠标悬停时显示。类似于Facebook

    1 回复  |  直到 16 年前
        1
  •  3
  •   user113716    16 年前

    $(this) 指的是你的 .post 元素。

    .filter() 删除与选择器不匹配的任何内容。

    所以在您的示例中,如果 帖子 元素不是下列元素之一

    'anything here,a,div,.class,#id'
    

    它会被过滤掉。

    过滤器() 不横穿。它接受一个jquery集,并将其减少到与给定选择器匹配的元素。

    http://api.jquery.com/filter/


    编辑:

    在jquery中有很多方法可以遍历。

    http://api.jquery.com/category/traversing/

    得到所有 a 元素的后代 帖子 接收事件的元素,可以执行以下操作:

    $(this).find('a');
    

    使用哪种遍历方法将取决于您的情况。