国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

js操作DOM--添加、刪除節點的簡單實例

2019-11-20 09:32:05
字體:
來源:轉載
供稿:網友
js removeChild() 用法<body> <p id="p1">welcome to <b>javascript</b> world !</p> <script language="javascript" type="text/javascript"> <!-- function nodestatus(node) {  var temp="";  if(node.nodeName!=null)  {   temp+="nodeName="+node.nodeName+"/n";  }  else temp+="nodeName=null /n";  if(node.nodeType!=null)  {   temp+="nodeType="+node.nodeType+"/n";  }  else temp+="nodeType=null /n";  if(node.nodeValue!=null)  {   temp+="nodeValue="+node.nodeValue+"/n";  }  else temp+="nodeValue=null /n";  return temp; } var parent=document.getElementById("p1"); var msg="父節點 /n"+nodestatus(parent)+"/n"; //返回元素節點p的最后一個孩子 last=parent.lastChild; msg+="刪除之前:lastChild--"+nodestatus(last)+"/n"; //刪除節點p的最后一個孩子,變為b parent.removeChild(last); last=parent.lastChild; msg+="刪除之后:lastChild--"+nodestatus(last)+"/n"; alert(msg); --> </script> </body> 
<html><head><title>js控制添加、刪除節點</title></head><script type="text/javascript">  var all;  function addParagraph() {    all = document.getElementById("paragraphs").childNodes;    var newElement = document.createElement("p");    var seq = all.length + 1;        //創建新屬性    var newAttr = document.createAttribute("id");    newAttr.nodeValue = "p" + seq;    newElement.setAttribute(newAttr);        //創建文本內容    var txtNode = document.createTextNode("段落" + seq);        //添加節點    newElement.appendChild(txtNode);    document.getElementById("paragraphs").appendChild(newElement);  }  function delParagraph() {    all = document.getElementById("paragraphs").childNodes;    document.getElementById("paragraphs").removeChild(all[all.length -1]);  }</script><style>  p{    background-color : #e6e6e6 ;  }</style><body><center>  <input type="button" value="添加節點" onclick="addParagraph();"/>  <input type="button" value="刪除節點" onclick="delParagraph();"/>  <div id="paragraphs">    <p id="p1">段落1</p>    <p id="p2">段落2</p>  </div></center></body></html>

以上這篇js操作DOM--添加、刪除節點的簡單實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 山东省| 吉安市| 手游| 扎兰屯市| 沅江市| 三门峡市| 苍梧县| 宁化县| 枞阳县| 阿瓦提县| 东莞市| 长岭县| 斗六市| 宁安市| 嘉黎县| 房产| 公主岭市| 邹城市| 长寿区| 沙坪坝区| 威远县| 图们市| 轮台县| 永寿县| 龙南县| 乌苏市| 冕宁县| 镇宁| 获嘉县| 三江| 平泉县| 谷城县| 盐亭县| 巴塘县| 永宁县| 都江堰市| 连城县| 时尚| 柞水县| 凉城县| 民权县|