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