jQuery Style How to - Highlight children items by index, change its background








Question

We would like to know how to highlight children items by index, change its background.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.5.2.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from www .j  a v a  2s  .  c  o  m-->
    $('div p').eq(2).css('background','yellow');
});
</script>
</head>
<body>
  <div>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
    <p>test</p>
  </div>
</body>
</html>

The code above is rendered as follows: