本文共 1944 字,大约阅读时间需要 6 分钟。
jquery 后代元素
In this post, we are going to discuss about an important jQuery traversing method to find all the descendents of an element. jQuery API provides find()
method to carry out this task.
在本文中,我们将讨论一种重要的jQuery遍历方法,以查找元素的所有后代。 jQuery API提供了find()
方法来执行此任务。
The jQuery find() method returns all the descendant elements like child, grandchild and so on. This method is used if you want to search all the elements deeper in the DOM tree. This method traverses downwards and finds all its descendants of the selected element. This method takes a mandatory filtering parameter to narrow down the search.
jQuery find()方法返回所有后代元素,例如child,孙子等等。 如果要搜索DOM树中更深的所有元素,则使用此方法。 此方法向下遍历并找到所选元素的所有后代。 此方法采用强制性的过滤参数来缩小搜索范围。
Here is the general syntax for using find() method:
这是使用find()方法的一般语法:
filter is the mandatory parameter passed to the find() method. filter could be any string selector expression, an element or a jQuery object.
filter是传递给find()方法的必需参数。 filter可以是任何字符串选择器表达式,元素或jQuery对象。
Following example demonstrates the jQuery find() method usage.
以下示例演示了jQuery find()方法的用法。
jQuery Traversing find jQuery find() demo
divp - I am div's child
spanp - I am span's Child and div's Grand Child
In this example, you can see two p
elements, one element is div
element’s child and the other one is it’s grandchild. The find() method finds all the p
element of the selected div
element and changes the color to red. Below image shows the output produced by above HTML page.
在此示例中,您可以看到两个p
元素,一个元素是div
元素的子元素,另一个元素是其孙子元素。 find()方法查找所选div
元素的所有p
元素,并将颜色更改为红色。 下图显示了以上HTML页面产生的输出。
You can use selector.find("*")
to find all the descendants of the HTML document.
您可以使用selector.find("*")
查找HTML文档的所有后代。
That’s all for now and you can find more traversing methods in the coming posts.
到此为止,您可以在以后的文章中找到更多的遍历方法。
翻译自:
jquery 后代元素
转载地址:http://zoqzd.baihongyu.com/