JS(六)DOM模型之節點

來源:互聯網
上載者:User

標籤:javascript   對象   dom   節點   

一、簡介

通過js dom找到某個元素,對該元素增加子節點,或刪除該元素的子節點。

二、樣本

1.動態增加和刪除節點元素

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta content="text/css" charset="utf-8" /><style>button {width: 100px;height: 50px;background:GREEN;}div{width:1500px;height:500px;background:red;margin-top:100px;}</style></head><body><button  onclick="addEle()">添加</button><button  onclick="deleteEle()">刪除</button><div id="div1">   <p id="p0">段落1</p></div></body><script>var x= 0;   //定義一個全域的變數function addEle(){    var ele = document.createElement("p");    var node = document.createTextNode("這個是一個段落");    x++;    ele.id="p" +x;  //對該節點元素設定id    ele.style.color="white";//設定節點元素的顏色    ele.appendChild(node);    var ele_parent = document.getElementById("div1");    ele_parent.appendChild(ele);}function deleteEle(){    var ele_parent = document.getElementById("div1");    var ele_child = document.getElementById("p"+x);    x--;    ele_parent.removeChild(ele_child);}</script></html>

運行效果:


2.動態添加多個節點

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta content="text/css" charset="utf-8" /><style>button {width: 100px;height: 50px;background: GREEN;}div {width: 500px;height: 3500px;background: red;margin-top: 100px;}</style></head><body><button onclick="addEle()">添加</button><button onclick="deleteEle()">刪除</button><div id="div1"></div></body><script>/** * <div> *      <img src="./header" width="500" height="100" /> *      <br/> *      <a href="http://xxx">風景圖片</a> * </div> */var x= 0;   //定義一個全域的變數function addEle(){    x++;    var ele = document.createElement("img");//建立img節點    ele.id="img" +x;  //對該節點元素設定id    ele.src="./header.jpg";    ele.width="500";    ele.height="100";    var eleBr=document.createElement("br");//建立br節點    eleBr.id="br" +x;    var ele2 = document.createElement("a");//建立a節點    ele2.id="a" + x;    ele2.href="http://www.baidu.com";    var node2 = document.createTextNode("風景圖片"+x);    ele2.appendChild(node2);    var ele_parent = document.getElementById("div1");    ele_parent.appendChild(ele);    ele_parent.appendChild(eleBr);    ele_parent.appendChild(ele2);}function deleteEle(){    var ele_parent = document.getElementById("div1");    var ele_child = document.getElementById("img"+x);    var ele_child2=document.getElementById("br" +x);    var ele_child3 = document.getElementById("a" +x);    ele_parent.removeChild(ele_child3);    ele_parent.removeChild(ele_child2);    ele_parent.removeChild(ele_child);    x--;}</script></html>


運行效果:




JS(六)DOM模型之節點

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.