javaScript_BOM_增加删除节点

<!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" οnclick="createNode();" value="开始测试">
  <input type="button" οnclick="removeNode();" value="删除节点">
  <input type="button" οnclick="inerrTest();" value="取得内容">
  <input type="button" οnclick="attrTest();" value="取得属性">
  <input type="button" οnclick="attrModify();" value="修改属性">
 </from>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值