var mystr="hello"; document.write(mystr+"<br>");//輸出hello後,輸出一個換行符
document.write(" 1 2 3 ");//只會有一個空格 結果: 1 2 3
document.write("aa"+" "+"的忠實粉絲!");
var mychar= document.getElementById('id');
var mynode= document.getElementsByName('myt') console.log(mynode.length) console.log(mynode[0]) <input name="myt" type="text" value="1"> <input name="myt" type="text" value="2"> <input name="myt" type="text" value="3"> <input name="myt" type="text" value="4"> <input name="myt" type="text" value="5"> <input name="myt" type="text" value="6">
<div class="con">11</div> <div class="con">22</div> <div class="con">33</div> <script type="text/javascript"> var divs = document.getElementsByClassName('con') console.log(divs.length) </script>
<div id="container"> <p class="name">111</p> <p class="name">222</p> <p class="name">333</p> </div> <script type="text/javascript"> var item = document.querySelector('#container .name') // <p class="name">111</p> console.log(item) </script>
<div id="container"> <p class="name">111</p> <p class="name">222</p> <p class="name">333</p> </div> var item = document.querySelectorAll('#container .name') // 3 console.log(item.length) // <p class="name">111</p> console.log(item[0])
<div> <p id="name">javascript</p> <div>jQuery</div> <h5>PHP</h5> </div> <script type="text/javascript"> var aa = document.getElementsByTagName('div')[0].childNodes; // 7 console.log(aa.length) // #text, ,3 文本節點(空白) // p, null, 1 元素節點 // #text, , 3 文本節點(空白) // div, null, 1 元素節點 // #text, ,3 文本節點(空白) // H5, null, 1 元素節點 // #text, , 3 文本節點(空白) for (var i = 0; i < aa.length; i++) { console.log(aa[i].nodeName + ',' + aa[i].nodeValue + ',' + aa[i].nodeType); } </script>
<div> <p id="name">javascript</p> <div>jQuery</div> <h5>PHP</h5> </div> <script type="text/javascript"> var aa = document.getElementsByTagName('div')[0].children; // p, null, 1 元素節點 // div, null, 1 元素節點 // H5, null, 1 元素節點 for (var i = 0; i < aa.length; i++) { console.log(aa[i].nodeName + ',' + aa[i].nodeValue + ',' + aa[i].nodeType); } </script>
<div id="text"> <p id="con"> parentNode 獲取指點節點的父節點</p> </div> <script type="text/javascript"> var mynode = document.getElementById("con"); // DIV console.log(mynode.parentNode.nodeName) </script>
function get_nextSibling(n){ var x=n.nextSibling; while (x && x.nodeType!=1){ x=x.nextSibling; } return x; }
<div> <p id="name">javascript</p> <div>jQuery</div> <h5>PHP</h5> </div> <script type="text/javascript"> var aa = document.getElementsByTagName('div')[0].childNodes; // 7 console.log(aa.length) // #text, ,3 文本節點(空白) // p, null, 1 元素節點 // #text, , 3 文本節點(空白) // div, null, 1 元素節點 // #text, ,3 文本節點(空白) // H5, null, 1 元素節點 // #text, , 3 文本節點(空白) for (var i = 0; i < aa.length; i++) { console.log(aa[i].nodeName + ',' + aa[i].nodeValue + ',' + aa[i].nodeType); } </script>
for (var i = 0; i < aa.length; i++) { if(aa[i].nodeName == 'P'){ // 子節點 var ps = aa[i].childNodes // 1 console.log(ps.length) // #text,javascript,3 for(let j = 0; j < ps.length; j++){ console.log(ps[j].nodeName + ',' + ps[j].nodeValue + ',' + ps[j].nodeType) } var attrs = aa[i].attributes // 1 console.log(attrs.length) // id,name,2 for(let j = 0; j < attrs.length; j++){ console.log(attrs[j].nodeName + ',' + attrs[j].nodeValue + ',' + attrs[j].nodeType) } } }
// 文檔節點 // #document undefined 9 console.log(document.nodeName,document.nodeValue,document.nodeType) for (var i = 0; i < document.childNodes.length; i++) { var item = document.childNodes[i] // html undefined 10 文檔聲明節點 // HTML undefined 1 html元素節點 console.log(item.nodeName,item.nodeValue,item.nodeType) } // BODY undefined 1 BODY節點 console.log(document.body.nodeName,document.body.nodeValue,document.body.nodeType) // HEAD undefined 1 HEAD節點 console.log(document.head.nodeName,document.head.nodeValue,document.head.nodeType)
<div id="div1"> <span>sss</span> </div> <script type="text/javascript"> var mychar = document.getElementById('div1'); // 網頁上顯示dddd mychar.innerHTML = '<span>dddd</span>' // <span>dddd</span> console.log(mychar.innerHTML); // 網頁上顯示<span>dddd</span> mychar.innerText = '<span>dddd</span>' // <span>dddd</span> console.log(mychar.innerText); </script>
mychar.style.color='red'; mychar.style.fontSize='20'; mychar.style.backgroundColor='blue'; mychar.style.height='30px'; mychar.style.width='250px'; mychar.style.display = 'block';
var p1 = document.getElementById("p1"); p1.className="one";
elementNode.getAttribute(name)javascript
elementNode.setAttribute(name,value)php
object.removeAttribute("style");css
<div id="div1" class="top" style="width: 100px;"> <span>sss</span> </div> <script type="text/javascript"> var mychar = document.getElementById('div1'); var attrs = mychar.attributes // 3 console.log(attrs.length) // id // class // style for(let i = 0; i < attrs.length; i++){ console.log(attrs[i]) } </script>
<script type="text/javascript"> var body = document.body; var input = document.createElement("input"); input.type = "button"; input.value = "建立一個按鈕"; body.appendChild(input); </script>
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>無標題文檔</title> </head> <body> <ul id="test"> <li>JavaScript</li> <li>HTML</li> </ul> <script type="text/javascript"> var otest = document.getElementById("test"); var add = document.createElement('li'); add.innerHTML = "PHP"; otest.appendChild(add); </script> </body> </html>
<div id="div1"> <p id="p1">aaa</p> </div> <div id="div2"></div> <script type="text/javascript"> var div1 = document.getElementById('div1') var div2 = document.getElementById('div2') var p1 = document.getElementById('p1') div2.appendChild(p1) </script>
<div id="div1"> <div id="div2">2222</div> </div> <script type="text/javascript"> var div1 = document.getElementById('div1') var div2 = document.getElementById('div2') var div3 = document.createElement('div'); div3.innerHTML = '3333' // div3 div2並列 div1.appendChild(div3); // div3在div2裏面,而且以前並列的div3不見了 //div2.appendChild(div3); </script>
<ul id="test"> <li>JavaScript</li> <li>HTML</li> </ul> <script type="text/javascript"> var otest = document.getElementById("test"); var add = document.createElement('li'); add.innerHTML = "PHP"; otest.insertBefore(add, otest.childNodes[2]); </script>
<div id="content"> <h1>html</h1> <h1>php</h1> <h1>javascript</h1> <h1>jquery</h1> <h1>java</h1> </div> <script type="text/javascript"> var content = document.getElementById("content"); for (var i = content.childNodes.length - 1; i >= 0; i--) { // 倒敘刪除 var childNode = content.childNodes[i]; content.removeChild(childNode); } </script> <button onclick="clearText()">清除節點內容</button>
<div> <b id="oldnode">JavaScript</b>是一個很經常使用的技術,爲網頁添加動態效果。 </div> <a href="javascript:replaceMessage()"> 將加粗改成斜體</a> <script type="text/javascript"> function replaceMessage() { var oldnode = document.getElementById("oldnode"); var oldHTML = oldnode.innerHTML; var newnode = document.createElement("i"); oldnode.parentNode.replaceChild(newnode, oldnode); newnode.innerHTML = oldHTML; } </script>
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>無標題文檔</title> <style type="text/css"> .message { width: 200px; height: 100px; background-color: #CCC; } </style> </head> <body> <script type="text/javascript"> var element = document.createElement("p"); element.className = "message"; var textNode = document.createTextNode("I love JavaScript!"); element.appendChild(textNode); document.body.appendChild(element); </script> </body> </html>
<div id="div1"> <p id="p1">aaa</p> </div> <div id="div2"></div> <script type="text/javascript"> var div1 = document.getElementById('div1') var div2 = document.getElementById('div2') var p1 = document.getElementById('p1') div2.appendChild(p1) </script>
<div id="div1"> <p id="p1">aaa</p> </div> <div id="div2"></div> <script type="text/javascript"> var div1 = document.getElementById('div1') var div2 = document.getElementById('div2') var p1 = document.getElementById('p1') var p2 = p1.cloneNode(true); div2.appendChild(p2) </script>