JS 增添或删除元素

首先,获取 body 元素 的方法有:
     1. doucumnet.body
     2. document.getElementsByTagName("body")[0]
 

增添元素:

     var Pic = document.createElement("img");

    Pic.setAttribute("src", "img/" + index + "-big.png");
    Pic.className = "showpic";     //增加类
    Pic.setAttribute("position", "absolute");
    Parent.appendChild(Pic);

 
删除元素:
     var showpic = document.getElementsByClassName("showpic")[0];

            showpic.parentNode.removeChild(showpic);

注意:getElementsByClassName 获取的是一个数组,不能对数组使用 removeChild()
posted @ 2015-04-27 10:54  Bestend  阅读(334)  评论(0编辑  收藏  举报