It can be seen that the value of parent is very clear, which is the parent element of the current element; parents is the ancestor element of the current element. Examples are listed below:
<p id='p1'> <p id='p2'><p></p></p> <p id='p3' class='a'><p></p></p> <p id='p4'><p></p></p> </p>
$('p').parent() gets p2, p3, p4
$('p').parent('.a ') gets p3
$('p').parent().parent() gets p1, which is rather strange; but JqueryObject Its own characteristics determine that this is feasible.
$('p').parents() gets p1, p2, p3, p4
$('p').parents('.a') gets p3
parent (exp) Usage: Get an element set containing the unique parent element of all matching elements.
<script src="jquery-1.2.6.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function() { $("#btn1").click(function(){ alert($(this).parent().next().html()); }); }); </script> </head> <body> <table> <tr> <td><input id="btn1" class="btn" type="button" value="test" /></td> <td>some text</td> </tr> </table>
Among them:
this.parent() is the td in front of the input
this.parent().parent() obtains tr
this.parent().parent() .parent() obtains table
this.parent().next() obtains td
adjacent to td. In the example:
<p><p>Hello</p><p>Hello</p></p>
$("p") .parent() gets: <p><p>Hello</p><p>Hello</p></p> object, because the parent tag of the p tag is p
Using jquery’s parents()
I encountered an interesting problem today. jquery has two functions parent() and parents(). Through these two functions, you can find the parent object of an object. Also known as jquery's selector. For example:
<body> <div id="one"> <div id="two">hello</div> <div id="three"> <p> <a href="#">tonsh</a> </p> </div> </div>
$("a").parent() will get the parent object
$("a").parents() will get the parent object
var id=$("a").parents("div:eq(1)").children("div:eq(0)").html(); alert(id);
The above is the detailed content of Share the difference between parent and parents in jquery. For more information, please follow other related articles on the PHP Chinese website!