【问题标题】:jQuery filter() traversing doesnt seems to work?jQuery filter() 遍历似乎不起作用?
【发布时间】:2010-05-29 17:20:42
【问题描述】:

我不知道这是什么问题?

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

因为这很好用。

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

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

【问题讨论】:

  • $(this) 的上下文是什么?你确定它有你要过滤的东西吗?
  • 另外,总而言之,您可能需要执行此操作两次,一次打开,一次关闭,并明确使用显示/隐藏。如果光标移动得足够快,它可能无法赶上。

标签: jquery filter traversal


【解决方案1】:

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

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

所以在您给定的示例中,如果 .post 元素不是以下元素之一

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

它被过滤掉了。

.filter() 不会遍历。它接受一个 jQuery 集合并将其缩减为与给定选择器匹配的元素。

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


编辑:

在jQuery中有很多遍历方式。

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

要获取作为接收事件的.post 元素的后代的所有a 元素,您可以这样做:

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

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

【讨论】:

  • 我知道了,它使用 children();感谢您的知识
  • $(this) 指的是收到事件的.post 元素。要访问$(this) 的后代,您可以执行$(this).find('a')。我会更新我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-27
  • 2011-01-14
相关资源
最近更新 更多