jQuery Selector How to - Select only direct descendant of body tag








Question

We would like to know how to select only direct descendant of body tag.

Answer


<!--from   w  w w .  java 2 s. c  om-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-2.1.0.js'></script>
<script type='text/javascript'>
$(window).load(function(){
    $('body > div.container').text('foo')
});
</script>
</head>
<body>
<body>
  <div>
    <div class='container'></div>
  </div>
  <div class='container'></div>
</body>
</body>
</html>

The code above is rendered as follows: