0

Possible Duplicate:
$(‘&lt;style></style>’).text(‘css’).appendTo(‘head’) does not work in IE?

I have this code:

$("head").append($("<style></style>")
  .attr("type", "text/css")
  .text("some text")
);

It works well with Firefox.

But with IE8, there is an error in the jQuery library:

access to the method or properties unexpected

The problem happens when I add the text function (whatever the text is).

4

2 回答 2

1

innerHTML is read-only for certain tags in IE - style is one of them. Here is a well written answer to a similar question with sources.

Create your entire style block and append it to the head in one shot

$('head').append('<style type="text/css">some text</style>');
于 2012-11-30T13:20:04.120 回答
0

Why don't you try this:

$("head").append($("<style></style>")
  .attr("type", "text/css")
  .html("some text")
);

Try this may be:

$( "<style>body { background: black; }</style>" ).appendTo( "head" );
于 2012-11-30T13:07:29.530 回答