DOM 文檔對象,dom文檔對象

來源:互聯網
上載者:User

DOM 文檔對象,dom文檔對象

一、HTMLCollection對象

<1>

getElementById('')
getElementsByTagName('');
getElementsByName('');

<2>
document.anchors
document.forms
document.images
document.links

<1>和<2>返回的均是HTMLCollection對象

namedItem('')方法
var myImg = images.namedItem('small');
var myImg = images['small'];
二者等價


二、HTMLElement對象操作

ele.innerHTML;//設定內部htmlele.innerText;//設定內部textele.appendChild(child);//尾部加入子節點ele.insertBefore(child, null);//等價於ele.appendChild(child);ele.insertBefore(child, benchmarkNode);//在基準節點之前插入子節點(benchmarkNode為基準節點)insertAfter(ele, child, benchmarkNode);//在基準節點之後插入子節點(需要自己實現)ele.replaceChild(child, oldNode);//替換子節點ele.removeChild(child);//移除節點ele.cloneNode();//節點複製(返回複製後的節點)document.createElement(ele);//建立元素節點document.createTextNode(txtEle);//建立文本節點


擴充:實現insertAfter方法
function insertAfter(ele, newNode, benchmarkNode){ele.insertBefore(newNode, benchmarkNode.nextSibling);}

三、節點遍曆

childNodes(包括所有 element和text節點 節點集合)firstChild 第一個子節點,等價於childNodes[0]lastChild 最後一個子節點,等價於childNodes[childNodes.length-1];parentNode 父節點(僅一個)nextSibling 同層級下一個兄弟節點previousSibling 同層級上一個兄弟節點

四、屬性的擷取、設定、遍曆

var mydiv = document.createElement('div');

<1>
mydiv.setAttribute("class", "ping");
console.log(mydiv.getAttribute("class"));

<2>
mydiv.className = "ping";
console.log(mydiv.className);

<3>
div.removeAttribute("class");//屬性移除

tip:
<1>可以設定包括自訂屬性在內的所有屬性,<2>不能設定自訂屬性。


attributes屬性

Element類型是使用attributes屬性的唯一一個DOM節點類型。
attributes屬性中包含一個NamedNodeMap,這是一個動態集合。
元素的每一個特性都由一個Attr節點表示,每個節點都儲存在NamedNodeMap對象中。


NamedNodeMap對象包含如下方法、屬性:

getNamedItem(name);
removeNamedItem(name);
setNamedItem(node);
item(pos);

attributes.length;

測試:

var mydiv = document.createElement('div');mydiv.setAttribute('id', 'wrap');mydiv.setAttribute('class', 'ping');mydiv.setAttribute('bg', 'yuyu');var oldAttr = mydiv.attributes.getNamedItem('bg');console.log(oldAttr);//yuyuvar newAttr = document.createAttribute('conn');newAttr.nodeValue = "link";mydiv.attributes.setNamedItem(newAttr);console.log(mydiv.attributes);console.log(item(2));


擴充:遍曆元素屬性

function outputAttribute(ele){var res = [];for(var i=0, len = ele.attributes.length; i < len; i++){var obj = {};obj.attrname = ele.attributes[i].nodeName;obj.attrvalue = ele.attributes[i].nodeValue;res.push(obj);}return res;}var mydiv = document.createElement('div');mydiv.setAttribute('id', 'wrap');mydiv.setAttribute('class', 'ping');mydiv.setAttribute('bg', 'yuyu');var attrs = outputAttribute(mydiv);console.log(attrs);

五、nodeType、nodeValue、nodeName
console.log(tagName == nodeName) //trueconsole.log(document.nodeType);//9console.log(document.nodeValue);//nullconsole.log(document.nodeName);//#documentvar mydiv = document.createElement('div');console.log(mydiv.nodeType);//1console.log(mydiv.nodeValue);//nullconsole.log(mydiv.nodeName);//DIV (預設大寫)var mytext = document.createTextNode('txt');console.log(mytext.nodeType);//3console.log(mytext.nodeValue);//txtconsole.log(mytext.nodeName);//#text

擴充一:
擷取節點的元素子節點集合(非文本節點):
通過 ele.childNodes[i].nodeType 的值來進行判斷

element: ele.childNodes[i].nodeType == 1
text: ele.childNodes[i].nodeType == 3
document: ele.childNodes[i].nodeType == 9

function getRealChildren(ele) {var real = [];for (var i = 0, len = ele.childNodes.length; i < len; i++) {if (ele.childNodes[i].nodeType == 1) {real.push(ele.childNodes[i]);}}return real;}

擴充二:
擷取子節點無文本節點的父節點對象:
通過遍曆去掉子節點中的文本節點

function getRealParentObj(ele){for (var i = 0; i < ele.childNodes.length; i++) {if (ele.childNodes[i].nodeType == 3) {ele.removeChild(ele.childNodes[i]);}}return ele;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.