zoukankan      html  css  js  c++  java
  • 【js】简单模拟JQuery原理

           
     
           /*
                    模拟工厂函数
                */
                function L(selector){
                    var eles = document.querySelectorAll(selector);
                    var obj =new LQuery();
                    eles.forEach(function(item,index){
                        obj[index]=item;
                    })
                    obj.length=eles.length;
                    return obj;
                }
                /*
                    模拟Jquery函数对象
                */
                function LQuery(){
                   
                }
                LQuery.prototype={
            setHtml:function(val){
                        if(this.length>0){
                            for(var i=0;i<this.length;i++){
                                this[i].innerHTML=val;
                            }
                        }else{
                            throw new DOMException("null pointer exception")
                        }
                        return this;
                    }
                }
          //测试
          window.onload=function(){
             L("div").setHtml("<h1>test</h1>")
           } 
     
     
          //html部分
      
          <div></div>
              <div></div>
  • 相关阅读:
    常用基础OC 集合
    字典的常用基本用法
    常用基础字符串常用基础实例
    软件测试:测试工程师的素质!
    软件测试:心得简介!
    java 汽车销售收入系统
    收银系统
    流程控制、循环结构
    java 聊天猜拳机器人
    设计模式(六)---建造模式
  • 原文地址:https://www.cnblogs.com/LightChan/p/8233254.html
Copyright © 2011-2022 走看看