jQuery“查找"方法替代 [英] jQuery "find" method alternative
问题描述
$('.wrapper a').find('a'); //return empty object
但是我正在寻找一种通过选择器获取所有锚点的方法.问题是查找方法仅查看后代,所以它的替代方法是什么?
But i am looking for a way get all anchors by selector. Problem is find method look at only descendants so what is alternative of it ?
请在 jsfiddle.net
推荐答案
jQuery find
获取当前匹配元素集中每个元素的后代,并通过选择器,jQuery对象或元素进行过滤.
jQuery find
gets the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.
children
获取匹配元素集合中每个元素的子元素,可以选择通过选择器进行过滤.
children
gets the children of each element in the set of matched elements, optionally filtered by a selector.
我认为您正在尝试在相同级别上查找元素,那么您应该使用children
.另外,您也可以使用filter
根据选择器过滤匹配的结果.
I think you are trying to find the elements at the same level then you should use children
. Alternatively you can also use filter
to filter the matched results based on selector.
filter
将匹配元素的集合减少为与选择器匹配或通过功能测试的元素.
filter
reduces the set of matched elements to those that match the selector or pass the function's test.
尝试一下
var div = $('.wrapper div').filter('.parent');
这篇关于jQuery“查找"方法替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!