Home > Article > Web Front-end > How to get the parent element using jquery? jquery method to get parent element
In fact, there are many ways to use jquery to get the parent, such as using parent(), parents(), closest(), these can help you find the parent element. Let’s talk about how to use jquery to get the parent. element? jquery method to get parent element.
1. Parent() method
In jQuery, we can use the parent() method to find the "parent element" of the current element. Remember, elements have only one parent.
Syntax: parent(expression)
Description: The parameter expression represents the jQuery selector expression, used to filter parent elements. When the parameter is omitted, all parent elements are selected. If the parameter is not omitted, the parent element that meets the conditions is selected.
Don’t the elements have only one parent element? Why is there still such a thing as "eligible parent element"? For this, take a look at the following example.
Example:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type="text/javascript" src="../App_js/jquery-1.12.0.min.js"></script> <script type="text/javascript"> $(function () { $("p").parent(".lvye").css("color", "red"); }) </script> </head> <body> <div><p>php中文网jQuery入门教程</p></div> <div class="lvye"><p>php中文网jQuery入门教程</p></div> <div><p>php中文网jQuery入门教程</p></div> </body> </html>
The effect is as follows:
## 2. parents() method
The parents() method is similar to the parent() method, both are used to find the ancestor elements of the selected element. But these two methods also have essential differences. In fact, these two methods are also easy to distinguish. parent is in singular form, and there is only one ancestor element to be searched for, which is the parent element. Parents is a plural form, and the ancestor elements to be searched are of course all ancestor elements. Syntax: parents(expression)Description: The parameter expression represents the jQuery selector expression string, used to filter ancestor elements. When the argument is omitted, all ancestor elements are selected. If the parameter is not omitted, the ancestor element that meets the conditions is selected. Example:<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type="text/javascript" src="../App_js/jquery-1.12.0.min.js"></script> <script type="text/javascript"> $(function () { $("#btn").click(function () { var parents = $("span").parents() .map(function () { return this.tagName; }) .get().join(","); alert("span元素的所有祖先元素为:" + parents.toLowerCase()); }); }) </script> </head> <body> <div><p><strong><span>jQuery入门教程</span></strong></p></div> <input id="btn" type="button" value="获取" /> </body> </html>The effect is as follows: ##3. parentsUntil() method
## The #parentsUntil() method is a supplement to the parents() method. It can find all ancestor elements in the specified range, which is equivalent to intercepting some ancestor elements from the collection returned by the parents() method.
Syntax: parents(expression)Description: The parameter expression represents the jQuery selector expression string, used to filter ancestor elements. When the argument is omitted, all ancestor elements are selected. If the parameter is not omitted, the ancestor element that meets the conditions is selected. The parameter selector represents the jQuery selector expression string, used to determine the ancestor elements of the range. This parameter is optional. If omitted, all ancestor elements will be matched, which is the same as the results of the parents() method. Since the parentsUntil() method is not used much, in order to reduce the memory burden of beginners, we can ignore it directly. If you want to learn more, you can refer to:jQuery Tutorial
.The above is the detailed content of How to get the parent element using jquery? jquery method to get parent element. For more information, please follow other related articles on the PHP Chinese website!