首页 > 关于jQuery的find()方法

关于jQuery的find()方法

看别人的代码发现了这样的写法:
$("#menu").find("[href =#item1]")
测试后发现确实可以通过对象的属性查找到所需的对象,但我查找了相关的资料似乎都没有写到这样的用法。
请问这样的写法有道理么?


我记得 find方法 就是找元素的属性的
如果你要找某个 类 也可以写成

.find(".some")


有,只要是选择器可以的写法,find基本都能找到


这是css的属性选择器


怎么会找不到。

首先找到find方法的API文档:http://api.jquery.com/find/

你会发现函数原型是这样的:
find( selector )Returns: jQuery

参数是Type: Selector,点击进去,到http://api.jquery.com/Types/#Selector页面看对选择器类型的介绍:

又连接到这个页面http://api.jquery.com/category/selectors/。其中就提到了属性相等性选择器啊:

Attribute Equals Selector [name=”value”]
Selects elements that have the specified attribute with a value exactly equal to a certain value.

所以,遇到问题一定要找官方文档啊。


Jquery 官方网站上有这个api的

find() 方法获得当前元素集合中每个元素的后代,通过选择器、jQuery 对象或元素来筛选。

$("#menu").find("[href =#item1]") ,找到id为menu元素下,href为 #item1的元素

【热门文章】
【热门文章】