javaScript_BOM_增加删除节点

来源:互联网 发布:linux传送文件命令 编辑:程序博客网 时间:2024/05/21 01:30
<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>documentss</title>
 <script type="text/javascript">
 function createNode() {//新增节点
  var img1=document.createElement("img");//动态创建图片元素
  img1.src="a/img.jpg";
  img1.id="img1";
  var fatherNode=document.getElementById("form1");
  fatherNode.appendChild(img1);//将图片元素添加到form1
 }
 function removeNode() {//删除节点
  var img1=document.getElementById("img1");//动态创建图片元素
  // var fatherNode=document.getEletmentById("form1");
  // fatherNode.removeChild(img1);//将图片元素删除
  img1.parentNode.removeChild(img1);//原理都是找到父节点删除子节点
 }
 function inerrTest(){
  var div1=document.getElementById("div1");
  alert(div1.innerText);// innerHTML
 }
 function attrTest() {
  var a1=document.getElementById("a1");
  alert(a1.getAttribute("href"));//取得超链接属性
 }
 function attrModify() {//修改元素属性
  var a1=document.getElementById("a1");
  a1.setAttribute("href","http://www.sina.com.cn");
  a1.innerText="新浪";
 }
 </script>
</head>
<body>
 <!-- 重点document是script重点 BOM对象树 -->
 <from id="form1">
  <div id="div1"  style="width: 200px;height: 100px">
   <h4>这是div的一个段落</h4>
   <a href="http:www.baidu.com" id="a1">百度</a>
  </div>
  <input type="button" onclick="createNode();" value="开始测试">
  <input type="button" onclick="removeNode();" value="删除节点">
  <input type="button" onclick="inerrTest();" value="取得内容">
  <input type="button" onclick="attrTest();" value="取得属性">
  <input type="button" onclick="attrModify();" value="修改属性">
 </from>
</body>
</html>
原创粉丝点击