实例代码“Ctrl+/”提示“F11/ESC”全屏 返回 格式化 恢复 运行
x
 
1
<!DOCTYPE html>
2
<html>
3
<head>
4
    <title>Dynamic Style Example 2</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
        function loadStyleString(css){
12
            var style = document.createElement("style");
13
            style.type = "text/css";
14
            try{
15
                style.appendChild(document.createTextNode(css));
16
            } catch (ex){
17
                style.styleSheet.cssText = css;
18
            }
19
            var head = document.getElementsByTagName("head")[0];
20
            head.appendChild(style);
21
        }
22
    
23
        function addStyle(){
24
            loadStyleString("body{background-color:red}"); 
25
        }
26
27
    </script>
28
</body>
29
</html>