jQuery文本属性和IE

时间:2012-11-30 12:55:25

标签: jquery internet-explorer

  

可能重复:
  $(‘<style></style>’).text(‘css’).appendTo(‘head’) does not work in IE?

我有这段代码:

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

适用于Firefox。

但是对于IE8,jQuery库中存在错误:

  

访问意外的方法或属性

当我添加文本功能(无论文本是什么)时,问题就出现了。

2 个答案:

答案 0 :(得分:1)

对于IE中的某些标记,

innerHTML是只读的 - 样式就是其中之一。以下是与来源类似问题的well written answer

创建整个样式块并一次性将其附加到头部

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

答案 1 :(得分:0)

为什么不试试这个:

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

试试这可能是:

$( "<style>body { background: black; }</style>" ).appendTo( "head" );