Differences between jquery parent and parents

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.