1.设置CSS值
<head> <meta charset="UTF-8"> <title>康释文</title> <script type="text/javascript"> function change() { var ce = document.getElementById("fenzhen"); ce.style.color = "blue"; ce.style.backgroundColor = "red"; ce.style.margin="20px"; } </script> </head> <body style="background-color:orange"> <div id="fenzhen"> <h1 style="color:blue; background-color: gray;margin:20px">绿叶学习网</h1> <input type="button" value="改变样式" οnclick="change()"/> </div> </body>
2.创建DOM节点
1) createElement()
<body style="background-color:orange"> <script type="text/javascript"> var body = document.body; var input = document.createElement("input"); input.value = "这是一个新创建的按钮"; body.appendChild(input); </script>
2) createTextNode()
<body style="background-color:orange"> <p id ="demo">单击按钮创建文本节点。</p> <button οnclick="myFunc()">点我</button> <script type="text/javascript"> function myFunc() { var t = document.createTextNode("HelloWorld"); document.body.appendChild(t); }; </script> </body>
3.创建节点的HTML
<body style="background-color:orange"> <div id="qianduan"></div> <script type="text/javascript"> document.all.qianduan.innerHTML="<span style= color:red;>前端开发</span><p style=color:blue;font-weight: bold;>js入门 </p"; </script> </body>