Home > Article > Web Front-end > How to get parent element node, child element node and sibling element node with jQuery_jquery
The example in this article describes how jQuery obtains parent element nodes, child element nodes, and sibling element nodes. Share it with everyone for your reference, the details are as follows:
Let’s look at this html code first. The entire method of getting nodes (father, son, brother, etc.) is based around this code:
<ul class="par"> <li id="firstli"> <h3 class="title">条目一</h3> <ul class="par"> <li id="dwtedx">第一项</li> <li>第二项</li> </ul> </li> </ul>
JQUERY parent node acquisition:
There are many ways to get parent elements in jquery, such as parent(), parents(), closest(), find, first-child, which can help you find parent nodes, child nodes, and sibling nodes
For example, we use parent() to get the parent node
$("#dwtedx").parent().parent();//取得id为firstli的li节点 $("#dwtedx").parent().parent(".par");//取得最上面的ul节点 $("#dwtedx").parent(".par"); //取得向上第一层的ul节点 以上是通过parent的方式来获取父节点的、大家可以根据自己项目需要来选择方法哈
Next, we will mainly talk about the difference between the parents() and closest() methods
1. closest starts matching and searching from the current element, and parents starts matching and searching from the parent element
2. Closest searches upwards step by step until it finds a matching element and then stops. Parents searches upwards until the root element, then puts these elements into a temporary collection, and then uses the given selector expression to Filter
3. closest returns 0 or 1 element, parents may contain 0, 1, or multiple elements
The code can be written like this:
$('#dwtedx').parents('.par');//可以找出所有class为.par的节点 $('#dwtedx').closest('.par'); //可以找出一个父节点、就是上面一层的那个
Getting JQUERY sibling nodes
JQuery sibling node acquisition, our idea is to find the parent node through the current node, and then find the child node through the parent node, the code is as follows
$(".title").parent().find('ul'); //找到自己的兄弟节点ul 就是通过先找到 h3 与 ul 共同的父节点 li 然后来用 find() 找到 ul
Another method is to use the siblings() function, the code is as follows
$(".title").siblings('ul'); //找到自己的兄弟节点ul
Getting JQUERY child nodes
Matches the first child element: first only matches one element, and this selector will match one child element for each parent element
$(".par:first-child"); //取得id为firstli的节点
Get it through the selector, the code is as follows:
$('#firstli h3.title'); //取得条目一的h3
Use the find() function, as mentioned above, the usage is the same
$("#firstli").find("h3"); //找到子兄弟节点h3
Use the children() function, the code is as follows
$("#firstli").children("h3.title"); //取得子节点h3、class为title
Readers who are interested in more jQuery-related content can check out the special topics on this site: "JQuery traversal algorithm and skills summary", "jQuery table (table) operation skills summary" , "Summary of jQuery drag effects and techniques", "Summary of jQuery extension techniques", "Summary of jQuery common classic special effects", "jQuery animation and special effects usage summary", "jquery selector usage summary" and "jQuery common plug-ins and usage summary"
I hope this article will be helpful to everyone in jQuery programming.