Get Kids node with JQuery

Source: Internet
Author: User

jquery provides a number of ways to get an element of the Direct descendant( immediate descendants ) 。 The simplest way is to use Direct descendantCombinator (>) For example, if you want to get the following HTML code <div id="content">的直接孩子节点中的a元素,就可以直接使用> 符号
<body><div id= "Content" ><a href= "http://www.jquery.com" >jquery</a><p><a href= " Http://blog.csdn.net/feelang ">jquery tutorial</a></p></div></body>

Use selectorGet
$ (' content > a ');


Of course, you can also use a jquery function with two parameters

$ (' > A ', ' #content ');


You can also invoke jquery's API in a chained style

$ (' #content '). Children ();


These three methods are all equivalent, but $ (' #content '). Children ()And $ (' content > a ')The difference is that the former query speed is greater than the latter.

It must take some time to parse selector on the surface, but this advantage is not absolute, but also depends on the internal implementation of the browser.

But using children () in this case is definitely an advantage.

var anchors = $ (' #content ')//Getting all Direct children of all anchor elements//can is achieved in three ways//#1ancho Rs.children ();//#2 $ (' > * ', anchors);//#3anchors. Find (' > * ')

Of course, the children () function also accepts selector parameters. For example

$ (' #content '). Children (' P ')


For more information, refer to jquery, three ways to get descendant elements

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.