JavaScript實現(xiàn)自動生成網(wǎng)頁元素功能(按鈕、文本等)
創(chuàng)建元素的方法:
- 1、利用createTextNode()創(chuàng)建一個文本對象
- 2、利用createElement()創(chuàng)建一個標簽對象
- 3、直接利用容器標簽中的一個屬性:innerHTML-----本質(zhì)上改該標簽容器中的“html代碼”,不是我們認為的對象樹的操作
詳解代碼:
<body> <input type="button" value="創(chuàng)建并添加節(jié)點1" onclick="addNode1()"/> <input type="button" value="創(chuàng)建并添加節(jié)點2" onclick="addNode2()"/> <input type="button" value="創(chuàng)建并添加節(jié)點3" onclick="addNode3()"/> <input type="button" value="remove節(jié)點1 " onclick='removenode()'/> <input type="button" value="replaceNode節(jié)點2替換 " onclick='remove2()'/><!--1替換2,并且1沒有保留--> <input type="button" value="clone替換 " onclick='clone()'/> <div id="div_id1">這是div模塊--</div> <div id="div_id2">必須好好地學習,這樣才能讓自己有很好的回報</div> <div id="div_id3">好好干,加油↖(^ω^)↗</div> <div id="div_id4">你懂得區(qū)域,實驗區(qū)域</div> </body>
方式一 :創(chuàng)建文本文檔
<span style="font-size:18px;">function addNode1(){ //1利用createTextNode()創(chuàng)建一個文本對象 var text=document.createTextNode("這是修改的,創(chuàng)建的文檔"); //2獲取div對象 var node1=document.getElementById("div_id1"); //添加成div對象的孩子 node1.appendChild(text);}</span><span style="font-size:24px;"> </span>
方式二:利用createElement()創(chuàng)建一個標簽對象
function addNode2(){ //1,利用createElement()創(chuàng)建一個標簽對象 var nn=document.createElement("input"); nn.type="button" nn.value="創(chuàng)建的按鈕"; nn.target="_blank"; //2,獲得div對象 var node2=document.getElementById("div_id2"); //添加成div對象的孩子 node2.appendChild(nn); }
方式三:直接利用容器標簽中的一個屬性:innerHTML-----本質(zhì)上改該標簽容器中的“html代碼”,不是我們認為的對象樹的操作
function addNode3(){ var mm=document.getElementById("div_id3"); mm.innerHTML="<a ><input type='button' value='新建的按鈕'></a>"; }
- 刪除節(jié)點
使用 removeNode 和 removeChild 從元素上刪除子結(jié)點兩種方法,通常采用第二種方法
function removenode(){ var node =document.getElementById("div_id4"); // alert(node.nodeName);//DIV // 自殺式 node.removeNode(true); //removeNode 從文檔層次中刪除對象。ie可以出現(xiàn)現(xiàn)象,一般不采用自殺式 node.parentNode.removeChild(node);////通過父節(jié)點去刪除它的孩子,一般常用 alert("aa"); }
- 替換 沒有保留替換的那個
function remove2(){ var node1 =document.getElementById("div_id1"); var node2 =document.getElementById("div_id2"); // node1.replaceNode(node2);//自殺式不通用 ////通過父節(jié)點去替換它的孩子:用node1去替換node2 node1.parentNode.replaceChild(node1,node2);//object.replaceChild(oNewNode, oChildNode) }
- clone節(jié)點
function clone(){ var node1 =document.getElementById("div_id1"); var node2 =document.getElementById("div_id2"); var node1_2=node1.cloneNode(true);//false只能clone基本的,不會clone下面的其他子節(jié)點 //克隆一個對象,默認參數(shù)為false。參數(shù)為true時,連子節(jié)點一起克隆 node1.parentNode.replaceChild(node1_2,node2); }
效果圖:
全部的源代碼:
<!DOCTYPE html> <html> <head> <title>DOM_operation.html</title> <style type="text/css"> div{ border:#00f solid 1px; width:200px; height:100px; } </style> <script type="text/javascript"> //AAAA 增 //方式一 創(chuàng)建文本文檔 function addNode1(){ //1利用createTextNode()創(chuàng)建一個文本對象 var text=document.createTextNode("這是修改的,創(chuàng)建的文檔"); //2獲取div對象 var node1=document.getElementById("div_id1"); //添加成div對象的孩子 node1.appendChild(text); } function addNode2(){ //1,利用createElement()創(chuàng)建一個標簽對象 var nn=document.createElement("input"); nn.type="button" nn.value="創(chuàng)建的按鈕"; nn.target="_blank"; //2,獲得div對象 var node2=document.getElementById("div_id2"); //添加成div對象的孩子 node2.appendChild(nn); } //直接利用容器標簽中的一個屬性:innerHTML-----本質(zhì)上改該標簽容器中的“html代碼”,不是我們認為的對象樹的操作 function addNode3(){ var mm=document.getElementById("div_id3"); mm.innerHTML="<a ><input type='button' value='新建的按鈕'></a>"; } //BBBBBB-------刪 //刪除節(jié)點 使用 removeNode 和 removeChild 從元素上刪除子結(jié)點兩種方法,通常采用第二種方法 function removenode(){ var node =document.getElementById("div_id4"); // alert(node.nodeName);//DIV // 自殺式 node.removeNode(true); //removeNode 從文檔層次中刪除對象。ie可以出現(xiàn)現(xiàn)象,一般不采用自殺式 node.parentNode.removeChild(node);////通過父節(jié)點去刪除它的孩子,一般常用 alert("aa"); } //替換 沒有保留替換的那個 function remove2(){ var node1 =document.getElementById("div_id1"); var node2 =document.getElementById("div_id2"); // node1.replaceNode(node2);//自殺式不通用 ////通過父節(jié)點去替換它的孩子:用node1去替換node2 node1.parentNode.replaceChild(node1,node2);//object.replaceChild(oNewNode, oChildNode) } function clone(){ var node1 =document.getElementById("div_id1"); var node2 =document.getElementById("div_id2"); var node1_2=node1.cloneNode(true);//false只能clone基本的,不會clone下面的其他子節(jié)點 //克隆一個對象,默認參數(shù)為false。參數(shù)為true時,連子節(jié)點一起克隆 node1.parentNode.replaceChild(node1_2,node2); } </script> </head> <body> <input type="button" value="創(chuàng)建并添加節(jié)點1" onclick="addNode1()"/> <input type="button" value="創(chuàng)建并添加節(jié)點2" onclick="addNode2()"/> <input type="button" value="創(chuàng)建并添加節(jié)點3" onclick="addNode3()"/> <input type="button" value="remove節(jié)點1 " onclick='removenode()'/> <input type="button" value="replaceNode節(jié)點2替換 " onclick='remove2()'/><!--1替換2,并且1沒有保留--> <input type="button" value="clone替換 " onclick='clone()'/> <div id="div_id1">這是div模塊--</div> <div id="div_id2">必須好好地學習,這樣才能讓自己有很好的回報</div> <div id="div_id3">好好干,加油↖(^ω^)↗</div> <div id="div_id4">你懂得區(qū)域,實驗區(qū)域</div> </body> </html>
以上就是為大家分享如何通過JavaScript實現(xiàn)自動生成網(wǎng)頁元素功能的方法,希望對大家的學習有所幫助。
相關(guān)文章
js實現(xiàn)鍵盤操作實現(xiàn)div的移動或改變的原理及代碼
實現(xiàn)鍵盤操作實現(xiàn)div的移動,最關(guān)鍵的一點:獲取div對象,下面有個不錯的示例,大家可以參考下2014-06-06

jquery插件bootstrapValidator數(shù)據(jù)驗證詳解

JS一維數(shù)組轉(zhuǎn)化為三維數(shù)組方法