CreateNode不工作IE9

时间:2014-01-13 13:28:14

标签: javascript dom internet-explorer-9

我在IE 9上的代码中收到错误。

    <!doctype html>
    <html>
    <head>
        <title>Create Node test</title>
    </head>
    <body>


    <script type="text/javascript">
    window.onload = function () {
        var head = document.getElementsByTagName('head')[0];
        var styleEl = document.createElement('style');
        var css = "body {};";

        if (styleEl.styleSheet){
          styleEl.styleSheet.cssText = css;
        } else {
          styleEl.appendChild(document.createTextNode(css));
        }

        head.appendChild(styleEl);
    }

    </script>
    </body>
    </html>

谁能告诉我我做错了什么?它在Chrome中运行良好。

由于

P.S。 要明确: 我想知道为什么这不起作用,我不想修复。我试图追踪IE 9中的错误,我认为这可能与createNode方法和样式元素有关。 有没有人之前有过这个问题,或者它是IE9中的一个已知错误?

1 个答案:

答案 0 :(得分:0)

您可以随时使用:

<!doctype html>
<html>
<head>
<title>Create Node test</title>
<script type="text/javascript" language="javascript">
window.onload = function () {
    var head = document.getElementsByTagName('head')[0];
    var styleEl = document.createElement('style');
    var css = "body {background-color:red;};";

    styleEl.innerHTML = css;

    head.appendChild(styleEl);
}
</script>
</head>
<body>
</body>
</html>
相关问题