js操作节点(添加、删除、更改属性)(转)

1.创建节点并添加内容:使用的方法:createElement和createTextNode

<html>
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>HTML DOM</title>
        <script type=text/javascript>
function Message()
        {
         var op=document.createElement("p");
         var oText=document.createTextNode("hello world!");
         op.appendChild(oText);
         document.body.appendChild(op);
        }
        </script>
</head>
<body οnlοad="Message();">
</body>
</html>

2,删除节点 方法:getElementsByTagName和removeChild

<html>
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>HTML DOM</title>
        <script type=text/javascript>
        function removeMessage()
        {
         var op=document.body.getElementsByTagName("p")[0];
         op.parentNode.removeChild(op);
        }
        </script>
</head>
<body οnlοad="removeMessage();">
        <p>hello world!</p>
</body>
</html>

3.替换节点 方法replace(new,old)

<html>
<head>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
      <title>HTML DOM</title>
<script type=text/javascript>
      function replaceMessage()
      {
       var oNewp=document.createElement("p");
       var oText=document.createTextNode("World Hello");
       oNewp.appendChild(oText);
       var oOldp=document.body.getElementsByTagName("p")[0];
       oOldp.parentNode.replaceChild(oNewp,oOldp);
      }
      </script>
</head>
<body οnlοad="replaceMessage();">
      <p>hello world!</p>
</body>
</html>

4.插入新消息 insertBefore(new,old)

<html>
<head>
     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
     <title>HTML DOM</title>
<script type=text/javascript>
     function replaceMessage()
     {
      var oNewp=document.createElement("p");
      var oText=document.createTextNode("World Hello");
      oNewp.appendChild(oText);
      var oOldp=document.body.getElementsByTagName("p")[0];
      document.body.insertBefore(oNewp,oOldp);
     }
     </script>
</head>
<body οnlοad="replaceMessage();">
     <p>hello world!</p>
</body>
</html>

5,文档碎片

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>HTML DOM</title>
<script type=text/javascript>
    /****************
     * 当向document中添加大量的节点时,如果逐个添加将会十分缓慢,这时可以使用文档碎片一次性添加到document中
     * 方法:createDocumentFragment()
     ********************************************/
    function replaceMessage()
    {
     var arrText=["first","second","third","fourth","fifth","sixth","seventh","eighth","ninth","tenth"];
     var oFragment=document.createDocumentFragment();//文档碎片
     for(var i=0;i<arrText.length;i++)
     {
      var op=document.createElement("p");
      var oText=document.createTextNode(arrText[i]);
      op.appendChild(oText);
      oFragment.appendChild(op);
     }
     document.body.appendChild(oFragment);
    }
    </script>
</head>
<body οnlοad="replaceMessage();">
    <p>hello world!</p>
</body>
</html>

6,操作document元素属性

<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
   <title>HTML DOM</title>
   <script type=text/javascript>
   function   Load_message()
   {
    var oimg=document.getElementById("a");
       alert(oimg.getAttribute("border"));
    oimg.setAttribute("alt","DOM Test");
   }
   </script>
</head>
<body οnlοad="Load_message();">
   <img border="0" width="100" height="150" id="a"/>
</body>
</html>

转载于:https://www.cnblogs.com/vbluebirdv/archive/2012/11/18/2776308.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值