jQuery Selector How to - Select all elements that are headers, like h1, h2, h3 and so on








Question

We would like to know how to select all elements that are headers, like h1, h2, h3 and so on.

Answer


<!-- www. ja v a 2s  .  c o  m-->
<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
      $(document).ready(function(){
         $(":header").css({ background:'#CCC', color:'blue' });
      });
    </script>
  </head>
  <body>
     <h1>Header 1</h1>
     <p>Contents 1</p>
     <h2>Header 2</h2>
     <p>Contents 2</p>
  </body>
</html>

The code above is rendered as follows: