jQuery“查找"方法替代 [英] jQuery "find" method alternative

查看:90
本文介绍了jQuery“查找"方法替代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$('.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆