现在的位置: 首页 > 综合 > 正文

模仿JQUERY写框架类

2013年08月17日 ⁄ 综合 ⁄ 共 633字 ⁄ 字号 评论关闭

 

var siren=window.siren=window.$=function(selector){
          return  new siren.fn.init(selector);
}

为了让我们生成的对象能够调用在prototype里定义出来的方法,
我们需要用原型的方式(把siren当作是一个类)给siren添加一些方法

 

 

siren.fn=siren.prototype={

//构造函数应该返回一个实例

    init:function(selector){
        if(selector==null){
            return this;
        }
        this[0]= document.getElementById(selector);
        this.length=1;
        return this
        //return this;
    },
    hello:function(){
        alert(this[0].value);   
    },
    a:function(){
        alert("测试");
    },
    b:function(){
        this.a();   
    }
   
   
}

初始化实例对象

siren.fn.init.prototype = siren.fn;

 

//页面调用

<input id="siren" value="11111111111" />

$("siren").hello();

抱歉!评论已关闭.