码迷,mamicode.com
首页 > Web开发 > 详细

jquery $选择符号的简单实现

时间:2016-04-05 15:40:41      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

全部代码

(function(){
function _$(els){
this.element = [];
for(var i = 0, len = els.length; i < len; i++){
var element = els[i],
typeClass = element.indexOf("."),
typeId = element.indexOf("#");
if(typeof element === ‘string‘){
if(typeId ==0){
element = document.getElementById(element);
}
if(typeClass == 0){
element = document.getElementsByClassName(element);
}
}
this.element.push(element);
}
return this;
}

_$.prototype = {
each: function(fn){
for(var i = 0, len = this.element.length; i < len; i++){
fn.call(this, this.element[i]);
}
return this;
},
setStyle: function(prop, val){
this.each(function(el){
el.style[prop] = val;
});
return this;
},
show: function(){
var that = this;
this.each(function(el){
that.setStyle(‘display‘, ‘none‘);
});
return this;
},

addEvent: function(type, fn){
var add = function(el){
if(window.addEventListener){
el.addEventListener(type, fn, false);
}else if(window.attachEvent){
el.attachEvent(‘on‘ + type, fn);
}
};
this.each(function(el){
add(el);
});
}
};

window.$ = function(){
return new _$(arguments);
}
})();

jquery $选择符号的简单实现

标签:

原文地址:http://www.cnblogs.com/halaileen/p/5354994.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!