实例代码“Ctrl+/”提示“F11/ESC”全屏 返回 格式化 恢复 运行
x
 
1
<!DOCTYPE html>
2
<html>
3
<head>
4
    <title>Dynamic Style Example</title>
5
</head>
6
<body>
7
    <p>You should see the background color change after clicking the button.</p>
8
    <input type="button" value="Add Style" onclick="addStyle()">
9
10
    <script type="text/javascript">
11
    
12
        function addStyle(){
13
            var style = document.createElement("style");
14
            style.type = "text/css";
15
            style.appendChild(document.createTextNode("body{background-color:red}"));  //error in IE
16
            var head = document.getElementsByTagName("head")[0];
17
            head.appendChild(style);
18
        }
19
20
    </script>
21
</body>
22
</html>