常用DOM整理
前言:
html為document搭建了一棵DOM樹,這棵樹就是有一系列Node節(jié)點(diǎn)所構(gòu)成的。他為我們定義了文檔的結(jié)構(gòu)。
Node類型:
Node.ELEMENT_NODE(1); //元素節(jié)點(diǎn)較常用
Node.ATTRIBUTE_NODE(2); //屬性節(jié)點(diǎn)較常用
Node.TEXT_NODE(3); //文本節(jié)點(diǎn)較常用
Node.CDATA_SECTION_NODE(4);
Node.ENTITY_REFERENCE_NODE(5);
Node.ENTITY_NODE(6);
Node.PROCESSING_INSTRUCTION_NODE(7);
Node.COMMENT_NODE(8);
Node.DOCUMENT_NODE(9); //文檔節(jié)點(diǎn)較常用
Node.DOCUMENT_TYPE_NODE(10);
Node.DOCUMENT_FRAGMENT_NODE(11);
Node.NOTATION_NODE(12);
相關(guān)函數(shù):
1、取得節(jié)點(diǎn):
document.getElementById('element');
document.getElementsByTagName('element'); 返回類數(shù)組對(duì)象
document.getElementsByName('element'); 返回類數(shù)組對(duì)象
document.getElementsByClassName('className') 返回類數(shù)組對(duì)象,IE7及以下并不支持;
document.querySelectorAll('class' | 'element') 返回類數(shù)組對(duì)象
2、遍歷節(jié)點(diǎn)
查找子節(jié)點(diǎn):element.childNodes 返回類數(shù)組對(duì)象
查找第一個(gè)子節(jié)點(diǎn):element.firstChild
查找最后一個(gè)子節(jié)點(diǎn):element.lastChild
查找父元素:element.parentNode
查找前一個(gè)兄弟元素: element.previousSibling
查找后一個(gè)兄弟元素: element.nextSibling
3、獲取節(jié)點(diǎn)信息
獲取元素或者屬性節(jié)點(diǎn)的標(biāo)簽名稱:elementNode.nodeName
獲取文本節(jié)點(diǎn)的內(nèi)容: textNode.nodeValue;
獲取并設(shè)置元素節(jié)點(diǎn)的內(nèi)容(可能會(huì)包含HTML標(biāo)簽): elementNode.innerHTML
獲取并設(shè)置元素節(jié)點(diǎn)的純文本內(nèi)容:element.innerText(IE) | element.textContent(FF)
獲取屬性節(jié)點(diǎn)的值: attrNode.getAttribute(AttrName);
設(shè)置屬性節(jié)點(diǎn)的值: attrNode.setAttribute(AttrName,AttrValue);
獲取節(jié)點(diǎn)的類型: node.nodeType;
元素節(jié)點(diǎn): 1;
屬性節(jié)點(diǎn): 2;
文本節(jié)點(diǎn): 3;
文檔節(jié)點(diǎn): 9;
注釋節(jié)點(diǎn): 8;
4、操作節(jié)點(diǎn)
創(chuàng)建元素節(jié)點(diǎn): document.createElement('element');
創(chuàng)建文本節(jié)點(diǎn): document.createTextNode('text');
創(chuàng)建屬性節(jié)點(diǎn): document.createAttribute('attribute');
刪除節(jié)點(diǎn): node.remove();
刪除子節(jié)點(diǎn): parentNode.removeChild(childNode);
插入節(jié)點(diǎn): parentNode.appendChild(childNode); //插入到父節(jié)點(diǎn)的尾部
parentNode.insertBefore(newNode,existingNode) //插入到已存在節(jié)點(diǎn)的前面;
克隆節(jié)點(diǎn): node.cloneNode([true]) //傳入true為深度復(fù)制
替換節(jié)點(diǎn): parentNode.replaceChild(newNode,oldNode);
相關(guān)拓展:
1、由于IE低版本瀏覽器和其他瀏覽器在處理DOM中存在不兼容,因此要做一些簡單的封裝處理。
//================= function getElementChildren(element) { var children = [], oldChildNodes = element.childNodes; for(var i=0, len=oldChildNodes.length; i<len; i+=1) { if(oldChildNodes[i].nodeType == 1) { children.push(oldChildNodes[i]); } } return children; } //================== function getElementNext(element) { var next = element.nextSibling || null; if(next) { if(next.nodeType == 1) { return next; } else { return arguments.callee(next); } } else { throw new Error("下一個(gè)兄弟元素不存在!"); } } //====================== function getElementPrev(element) { var prev = element.previousSibling || null; if(prev) { if(prev.nodeType == 1) { return prev; } else { return arguments.callee(prev); } } else { throw new Error("上一個(gè)兄弟元素不存在!"); } }
2、操作DOM元素的樣式
//========================= function getElementStyle(element,styleName) { if(typeof getComputedStyle != 'undefined') { return getComputedStyle(element,null)[styleName]; } else { return element.currentStyle[styleName]; } } //========================== function addClass(element,className) { element.className += className; } //========================== function removeClass(element,removeClassName) { var classStr = element.className; element.className = classStr.replace(removeClassName,'').split(/\s+/).join(' ').replace(/^\s+/,'').replace(/\s+$/,''); }
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
相關(guān)文章
JS數(shù)組方法reverse()用法實(shí)例分析
這篇文章主要介紹了JS數(shù)組方法reverse()用法,結(jié)合實(shí)例形式分析了JS數(shù)組reverse()方法基本功能、用法與操作注意事項(xiàng),需要的朋友可以參考下2020-01-01JS highcharts實(shí)現(xiàn)動(dòng)態(tài)曲線代碼示例
這篇文章主要介紹了JS highcharts實(shí)現(xiàn)動(dòng)態(tài)曲線代碼示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10微信小程序前后端數(shù)據(jù)交互的詳細(xì)圖文教程
這篇文章主要給大家介紹了關(guān)于微信小程序前后端數(shù)據(jù)交互的相關(guān)資料,通過小程序向后端發(fā)送請(qǐng)求,然后后端從數(shù)據(jù)庫獲取車源和求購的數(shù)量反饋給小程序,最后將這兩個(gè)數(shù)據(jù)顯示出來,需要的朋友可以參考下2022-10-10通過正則格式化url查詢字符串實(shí)現(xiàn)代碼
看到項(xiàng)目里通過js數(shù)組split方法格式化查詢字符串的,突發(fā)奇想為什么不能用正則呢,性能如何?感興趣的朋友可以研究下哦2012-12-12一個(gè)超簡單的JS拖拽實(shí)現(xiàn)代碼(兼容IE,Firefox)
網(wǎng)上找的一個(gè)超簡單的JS拖拽,喜歡拖拽效果的朋友可以參考下。2010-04-04JS 中document.write()的用法和清空的原因淺析
這篇文章主要介紹了JS 中document.write()的用法和清空的原因淺析,需要的朋友可以參考下2017-12-12