Child Selector (“parent > child”) in JQuery

 

Sample:

version added: 1.0jQuery(‘parent > child’)

  • parent
    Any valid selector.
    child
    A selector to filter the child elements.

As a CSS selector, the child combinator is supported by all modern web browsers including Safari, Firefox, Opera, Chrome, and Internet Explorer 7 and above, but notably not by Internet Explorer versions 6 and below. However, in jQuery, this selector (along with all others) works across all supported browsers, including IE6.

The child combinator (E > F) can be thought of as a more specific form of the descendant combinator (E F) in that it selects only first-level descendants.

Example:

Finds all children of the element with id “main” which is yellow.

<!DOCTYPE html>
<html>
<head>
  <style>
body { font-size:14px; }
</style>
  <script src="/scripts/jquery-1.4.js"></script>
</head>
<body>
	
                    
<ul class="topnav">
    <li>Item 1</li>
    <li>Item 2 
        <ul><li>Nested item 1</li><li>Nested item 2</li><li>Nested item 3</li></ul>
       </li>
    <li>Item 3</li>
</ul>

<script>$("ul.topnav > li").css("border", "3px double red");</script>
</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *