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>
  • 相关阅读:
    国内最火的3款前端开发框架
    Cordova是做什么的
    老师你好。使用cordova生成的hellowold 的安卓5.0版本太高。怎么才可以生成4.4的呢?
    一个类似bootstrap的foundation
    role在标签中的作用是什么?
    如何做到根据不同的进度用不同的颜色显示整个进度条
    wall 和panel有啥区别
    git ignore
    eclipse js 引用跳转
    计划
  • 原文地址:https://www.cnblogs.com/LightChan/p/8233254.html
Copyright © 2011-2022 走看看