zoukankan      html  css  js  c++  java
  • js:appendChild、insertBefore和insertAfter

    web/Win8开发中经常要在js中动态增加一些element,就需要用到下面的一些方法:

    appendChild:

    target.appendChild(newChild)

    newChild作为target的子节点插入最后的一子节点之后

    insertBefore:

    target.insertBefore(newChild,existingChild)

    newChild作为target的子节点插入到existingChild节点之前

    existingChild为可选项参数,当为null时其效果与appendChild一样

    insertAfter: 

    顾名思义,就是在node后面增加new node,但是没有现成的API提供调用,但也很容易的自己可以写:

    function insertAfter(newEl, targetEl)
    {
        var parentEl = targetEl.parentNode;
                
         if(parentEl.lastChild == targetEl)
         {
               parentEl.appendChild(newEl);
          }else
          {
               parentEl.insertBefore(newEl,targetEl.nextSibling);
           }            
    }
       


    作者:老Zhan
    出处:http://www.cnblogs.com/mybkn/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

     
  • 相关阅读:
    周总结5
    《梦段代码》阅读笔记01
    NABCD
    结对开发(四则运算)
    《人月神话》阅读笔记03
    周总结4
    移动端疫情显示
    周总结3
    《人月神话》阅读笔记02
    软件工程第四周作业
  • 原文地址:https://www.cnblogs.com/mybkn/p/3011061.html
Copyright © 2011-2022 走看看