动态内容的CSS

Aft*_*ton 1 css dynamic-data

我正在努力使用javascript将内容添加到网页.问题是IE(7)中的CSS似乎不适用于动态添加的内容.

这是一个示例文档..

<html>
<head>
    <style type="text/css">
    p.foo { color: #FF4400 ; background-color: #000000 }
    p.bar { color: #FF0000 ; background-color: #000000 }
    </style>
    <script type="text/javascript">
        function add() {
            var node = document.createElement("p");
            node.setAttribute("class", "bar");
            node.appendChild(document.createTextNode("New Content"));
            document.body.appendChild(node);
        };
    </script>
</head>
<body onload="add()">
        <p class="bar">bar</p>
        <p class="foo">foo</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

在FF中,新添加的"新内容"段落具有应用于它的样式,但在IE中,它没有.这看起来很明显,它应该很容易搜索到,但是一些明显的查询并没有给我任何东西.

那么诀窍是什么?

tva*_*son 9

为什么不使用已经解决了所有这些问题的框架,例如jQuery,MooTools,extJs,Dojo,Prototype等?

但如果您坚持自己动手,请尝试使用:

    function add() {
        var node = document.createElement("p");
        node.className = 'bar'; // <- use in leu of setAttribute()
        node.appendChild(document.createTextNode("New Content"));
        document.body.appendChild(node);
    };
Run Code Online (Sandbox Code Playgroud)