It works fine in Firefox and Chrome, but does not work in IE8. Here is the html structure:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
// this does not work in IE
$('<style type="text/css"></style>').text('body {margin: 0;}').appendTo('head');
});
</script>
</head>
<body>
</body>
</html>
And what’ s the alternative to do this in IE?
This is working for me in IE7:
Another syntax which might be easier to read:
However, calling either
.text(val)or.html(val)to set the contents of thestyletag will cause an exception to be thrown because they set theinnerHTMLDOM property which is read-only.Here is IE’s documentation of the
innerHTMLproperty: