zoukankan      html  css  js  c++  java
  • 常用dom方法封装

    // 封装一个方法能够在元素后面插入dom元素 (在原型链上编程)
     
    <div>
        <span></span>
        <p></p>
        <strong></strong>
        <i></i>
        <address></address>
    </div>
    

      

     
    Element.prototype.insertAfter=function(targetNode,afterNode){
        var beforeNode=afterNode.nextElementSibling;
       if(beforeNode==null){
             this.appendChild(targetNode)
        }else{
          this.insertBefore(targetNode,beforeNode);
        } 
    }
    
    
    var div=document.getElementsByTagName('div')[0];
    var strong=document.getElementsByTagName('strong')[0];
    var p=document.getElementsByTagName('p')[0];
    var ul=document.createElement('ul');
    
    div.appendChild(ul)
    Element.prototype.insertAfter=function(targetNode,afterNode){
      var beforeNode=afterNode.nextElementSibling;
      if(beforeNode==null){
        this.appendChild(targetNode)
      }else{
        this.insertBefore(targetNode,beforeNode);
      } 
    }
    
    var li=document.createElement('li');
    div.insertAfter(li,strong)
    // 遍历元素节点树(在原型链上编程)
    var a=[];
    Element.prototype.allElements=function(){
              
              var dom=this
              for(var i=0;i<dom.children.length;i++){
                  a.push(dom.children[i])
                  if(dom.children[i].hasChildNodes()){
                      // console.log(this.children[i].children[i])
    
                       dom.children[i].allElements();
                  }
                   
                   
              }
           return a;
    
        }
    
    console.log(div.allElements())
     
  • 相关阅读:
    《秒杀系统架构分析与实战 》
    《豆瓣的基础架构》
    转--《亿级用户下的新浪微博平台架构 》
    转-《蚂蚁金服11.11:支付宝和蚂蚁花呗的技术架构及实践 》
    hdu2029
    hdu2027
    hdu2026(water~~)
    PHP电影小爬虫(2)
    今天来做一个PHP电影小爬虫。
    PHP Simple HTML DOM解析器
  • 原文地址:https://www.cnblogs.com/h5it/p/13456521.html
Copyright © 2011-2022 走看看