本文實(shí)例講述了JavaScript中Dom操作。分享給大家供大家參考,具體如下:
博主將按照增刪改查的方式來介紹一下Dom的主要操作。
1、增加節(jié)點(diǎn)
添加節(jié)點(diǎn)的操作主要分為3步:
(1)創(chuàng)建要添加的新節(jié)點(diǎn)
(2)找到要添加到的父節(jié)點(diǎn)
(3)父節(jié)點(diǎn)添加新節(jié)點(diǎn)
參考代碼
<!DOCTYPE html><html><head>  <meta charset="utf-8">  <meta http-equiv="X-UA-Compatible" content="IE=edge">  <title></title>  <link rel="stylesheet" href="">  <script>    function add(){      //新建要增加的節(jié)點(diǎn)      var li = document.createElement('li');      var txt = document.createTextNode('北');      li.appendChild(txt);      //找到要添加到的父節(jié)點(diǎn)      var ul = document.getElementsByTagName('ul')[0];      //添加到父節(jié)點(diǎn)      ul.appendChild(li);    }  </script></head><body>  <input type="button" value="增加一個(gè)節(jié)點(diǎn)" onclick="add();">  <ul>    <li>東</li>    <li>南</li>    <li>西</li>  </ul></body></html>2、刪除節(jié)點(diǎn)
刪除節(jié)點(diǎn)的操作主要分為3步:
(1)先找到要?jiǎng)h除的節(jié)點(diǎn)
(2)找到其父節(jié)點(diǎn)
(3)調(diào)用父節(jié)點(diǎn)的removeChild(刪除的節(jié)點(diǎn))操作
參考代碼
<!DOCTYPE html><html><head>  <meta charset="utf-8">  <meta http-equiv="X-UA-Compatible" content="IE=edge">  <title></title>  <link rel="stylesheet" href="">  <script>    function del(){      //找到要?jiǎng)h除的節(jié)點(diǎn)      var lis = document.getElementsByTagName('li');      var lastli = lis[lis.length - 1];      //讓父節(jié)點(diǎn)來進(jìn)行刪除操作      lastli.parentNode.removeChild(lastli);    }  </script></head><body>  <input type="button" value="刪除最后一個(gè)節(jié)點(diǎn)" onclick="del();">  <ul>    <li>東</li>    <li>南</li>    <li>西</li>    <li>北</li>  </ul></body></html>3、修改節(jié)點(diǎn)
修改操作主要是修改節(jié)點(diǎn)的屬性,如修改img節(jié)點(diǎn)的src屬性
但是有幾點(diǎn)需要注意:
(1)如果需要操作dom對象的class屬性,應(yīng)該使用className屬性
document.getElementsByTagName('div')[0].className = 'test2';(2)如果需要修改dom對象的樣式,應(yīng)該使用 obj.style.css樣式名
document.getElementsByTagName('div')[0].style.width = parseInt(div.style.width) + 5 + 'px';(3)遇到有這樣帶-的樣式,如:border-bottom-width,應(yīng)這樣修改樣式,將-去掉,然后將-后的首字母改為大寫
div.style.borderBottomWidth = parseInt(div.style.borderBottomWidth) + 5 + 'px';
4、查找節(jié)點(diǎn)
(1)根據(jù)id進(jìn)行查找,返回對象:
document.getElementById('test1');(2)根據(jù)標(biāo)簽名進(jìn)行查找,返回對象集合:
document.getElementsByTagName('p');(3)對于表單元素,按照name值進(jìn)行查找,返回對象集合:
document.getElementsByName('username');(4)根據(jù)class名進(jìn)行查找,返回對象集合:
document.getElementsByClassName('test2');(5)查找對象的子節(jié)點(diǎn),childNodes屬性包括文本節(jié)點(diǎn)(換行),返回對象集合
alert(document.getElementById('test1').childNodes.length);//打印包括文本節(jié)點(diǎn)的數(shù)量alert(document.getElementById('test1').children.length);//children屬性雖然不符合w3c規(guī)范,但是瀏覽器支持良好(6)查找對象的父節(jié)點(diǎn),使用parentNode,返回單個(gè)對象
document.getElementsByTagName('p')[0].parentNode.style.border = '1px solid black';感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.VeVB.COm/code/HtmlJsRun測試上述代碼效果。
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript操作DOM技巧總結(jié)》、《JavaScript頁面元素操作技巧總結(jié)》、《JavaScript事件相關(guān)操作與技巧大全》、《JavaScript查找算法技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答