jQuery Selector How to - Select parents and grandchildren








Question

We would like to know how to select parents and grandchildren.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.10.1.js'></script>
<script type='text/javascript'>
$(function(){<!--from   w  w w  .  j  a  v a 2  s  . c  o  m-->
    $('.jstree a:not(:eq(1))').css('background', 'yellow');
});
</script>
</head>
<body>
  <div class="jstree">
    <ul>
      <li><a>PARENT</a>
        <ul>
          <li><a>CHILD</a>
            <ul>
              <li><a>GRANDCHILD</a></li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</body>
</html>

The code above is rendered as follows: