| [ Web Proxy ] |
| Viewing: https://raw.githubusercontent.com/appreturn/javascript-tutorial/master/docs/dom/node.md | [Back] [Original] |
GoodBye!
'; ``` `` textcommentattr`textContent``nodeValue` documentdoctype`textContent``null``document.documentElement.textContent` ### Node.prototype.baseURI `baseURI` URL ```javascript // // http://www.example.com/index.html document.baseURI // "http://www.example.com/index.html" ``` URL`baseURI``null` URL`window.location` HTML `` ```html ``` `baseURI``` ### Node.prototype.ownerDocument `Node.ownerDocument``document` ```javascript var d = p.ownerDocument; d === document // true ``` `document``ownerDocument``null` ### Node.prototype.nextSibling `Node.nextSibling``null` ```javascript // HTML //
First span
var p1 = document.getElementById('p1'); p1.firstChild.nodeName // "SPAN" ``` `p``span` `firstChild` ```javascript // HTML //// First span //
var p1 = document.getElementById('p1'); p1.firstChild.nodeName // "#text" ``` `p``span``firstChild` `lastChild``null``firstChild` ### Node.prototype.childNodes `childNodes``NodeList` ```javascript var children = document.querySelector('ul').childNodes; ``` `children``ul` ```javascript var div = document.getElementById('div1'); var children = div.childNodes; for (var i = 0; i < children.length; i++) { // ... } ``` documentdocType HTML ```javascript var children = document.childNodes; for (var i = 0; i < children.length; i++) { console.log(children[i].nodeType); } // 10 // 1 ``` 101 `childNodes``NodeList``NodeList` ### Node.prototype.isConnected `isConnected` ```javascript var test = document.createElement('p'); test.isConnected // false document.body.appendChild(test); test.isConnected // true ``` `test``isConnected``false``true` ## ### Node.prototype.appendChild() `appendChild()` ```javascript var p = document.createElement('p'); document.body.appendChild(p); ``` ```document.body` DOM `appendChild()` ```javascript var div = document.getElementById('myDiv'); document.body.appendChild(div); ``` `myDiv``document.body` `appendChild()``DocumentFragment``DocumentFragment``DocumentFragment``DocumentFragment` ### Node.prototype.hasChildNodes() `hasChildNodes` ```javascript var foo = document.getElementById('foo'); if (foo.hasChildNodes()) { foo.removeChild(foo.childNodes[0]); } ``` `foo` `hasChildNodes``true` - `node.hasChildNodes()` - `node.firstChild !== null` - `node.childNodes && node.childNodes.length > 0` `hasChildNodes``firstChild``nextSibling` ```javascript function DOMComb(parent, callback) { if (parent.hasChildNodes()) { for (var node = parent.firstChild; node; node = node.nextSibling) { DOMComb(node, callback); } } callback(parent); } // DOMComb(document.body, console.log) ``` `DOMComb` ### Node.prototype.cloneNode() `cloneNode` ```javascript var cloneUL = document.querySelector('ul').cloneNode(true); ``` 1`addEventListener``on-``node.onclick = fn` 2`Node.appendChild` 3DOM `id``id="xxx"``id``name` ### Node.prototype.insertBefore() `insertBefore` ```javascript var insertedNode = parentNode.insertBefore(newNode, referenceNode); ``` `insertBefore``newNode``parentNode``referenceNode``newNode``referenceNode``newNode` ```javascript var p = document.createElement('p'); document.body.insertBefore(p, document.body.firstChild); ``` `
``document.body.firstChild``document.body` `insertBefore``null` ```javascript var p = document.createElement('p'); document.body.insertBefore(p, null); ``` `p``document.body``insertBefore` DOM `insertAfter``insertBefore``nextSibling` ```javascript parent.insertBefore(s1, s2.nextSibling); ``` `parent``s1``s2``s1``s2``s2``s2.nextSibling``null``s1``s2` `DocumentFragment``DocumentFragment``DocumentFragment``DocumentFragment` ### Node.prototype.removeChild() `removeChild` ```javascript var divA = document.getElementById('A'); divA.parentNode.removeChild(divA); ``` `divA``divA``divA` ```javascript var element = document.getElementById('top'); while (element.firstChild) { element.removeChild(element.firstChild); } ``` DOM `removeChild` ### Node.prototype.replaceChild() `replaceChild` ```javascript var replacedNode = parentNode.replaceChild(newChild, oldChild); ``` `replaceChild``newChild``oldChild``oldChild` ```javascript var divA = document.getElementById('divA'); var newSpan = document.createElement('span'); newSpan.textContent = 'Hello World!'; divA.parentNode.replaceChild(newSpan, divA); ``` `divA` ### Node.prototype.contains() `contains` - - - ```javascript document.body.contains(node) ``` `node` `contains``true` ```javascript nodeA.contains(nodeA) // true ``` ### Node.prototype.compareDocumentPosition() `compareDocumentPosition``contains` | | ---------|------|----- 000000 | 0 | 000001 | 1 | 000010 | 2 | 000100 | 4 | 001000 | 8 | 010000 | 16 | 100000 | 32 | ```javascript // HTML //
| Web Proxy Viewer | New URL | Original Page |