jquery $选择符号的简单实现
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jquery $选择符号的简单实现相关的知识,希望对你有一定的参考价值。
全部代码
(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 $选择符号的简单实现的主要内容,如果未能解决你的问题,请参考以下文章