It can be seen that the value of parent is clear, that is, the parent element of the current element; parents is the ancestor element of the current element. The following are examples:
Copy codeThe Code is as follows:
<Div id = 'div1 '>
<Div id = 'div2'> <p> </div>
<Div id = 'did3' class = 'A'> <p> </div>
<Div id = 'div4 '> <p> </div>
</Div>
$ ('P'). parent () obtains div2, div3, div4
$ ('P'). parent ('. A') obtains div3.
$ ('P'). parent (). parent () gets div1, which is odd. However, the characteristics of Jquery objects determine that this is feasible.
$ ('P'). parents () obtains div1, div2, div3, and div4.
$ ('P'). parents ('. A') obtains div3.
Parent (exp) usage: gets a set of elements that contain a unique parent element that matches all elements.
Copy codeThe Code is as follows:
<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>
Where:
This. parent () is the td before input
This. parent (). parent () gets tr
This. parent () obtains table
This. parent (). next () obtains the td
In the example:
<Div> <p> Hello </p> </div>
$ ("P "). parent () obtains the <div> <p> Hello </p> </div> object, because the parent label of the p tag is a div