jQuery Method How to - Use css(): Create Javascript and assign to it








Question

We would like to know how to use css(): Create Javascript and assign to it.

Answer


<!--from w  w w  . j a v  a  2  s. co  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(){
              $("b").hover(function () {
                   $(this).css({'background-color' : 'yellow', 'font-weight' : 'bolder'});
              }, function () {
                   var cssObj = {
                     'background-color' : '#ddd',
                     'font-weight' : '',
                     'color' : 'red'
                   }
                   $(this).css(cssObj);
              });
        });
    </script>
  </head>
  <body>
       <b>Hello</b>
  </body>
</html>

The code above is rendered as follows: