jQuery Style How to - Create dynamic Style by adding style element to head element








Question

We would like to know how to create dynamic Style by adding style element to head element.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.5.2.js'></script>
<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){<!--   w w w  .  j a  v  a  2 s  .  c om-->
  $('head').append($("<style>span{color:red}</style>"));
});//]]>  
</script>
</head>
<body>
  <span>Test</span>
</body>
</html>

The code above is rendered as follows: