JQuery:obj.on('click',function(){...});
想将这个用原生js写出来,我自己也尝试着写了下:Object.prototype.on=function(eventName,fn){
document.all?this.attachEvent('on'+eventName,fn):this.addEventListener(eventName,fn,false);
};
var $=function(){return document.getElementById(arguments[0]);};
$('btn').on('click',function(){
alert('打印不出来');
});
得不到自己想要的结果,各位大侠看到的请各显神通,写出你们自己的js代码

解决方案 »

  1.   

    好好学习,天天向上
    function $(){
    return new $.prototype.init(arguments[0]);
    };
    $.prototype = {
    init: function() {
    this.obj = document.getElementById(arguments[0]) || null;
    },
    on: function(eventName, fn) {
    this.obj && (document.all? this.obj.attachEvent('on'+ eventName, fn) : this.obj.addEventListener(eventName, fn, false));
    }
    };
    $.prototype.init.prototype = $.prototype;$('btn').on('click', function(){
    alert('打印不出来');
    });
      

  2.   

    晕,傻了巴叽的
    this.obj = document.getElementById(arguments[0])//不要 || null;
      

  3.   

    不过在alert(this.id)的时候不会出现它的id值