原生js,插入元素

 1    function insert(){
 2              var tarDiv = document.getElementById("aId");
 3              var newDiv = document.createElement("div");
 4                  newDiv.innerHTML = "insert";
 5                  newDiv.id = "insert";
 6                  oInsert.insertAfter(newDiv, tarDiv);
 7           }
 8 
 9           var oInsert = {
10              insertAfter: function(newEl, targetEl){
11                  var parent = targetEl.parentNode;
12                  if(parent.lastChild == targetEl){
13                       parent.appendChild(newEl, targetEl);
14                  }else{
15                       parent.insertBefore(newEl, targetEl.nextSibling);
16                  }  
17              }
18  
19           }

 

posted @ 2014-11-03 17:18  未来动力  阅读(1459)  评论(0编辑  收藏  举报