// js的框架封装
let $ = jQuery = (function (window) {
function Query(dom, selector) {
let i, len = dom ? dom.length : 0;
for(i=0;i<len;i++){
this[i]=dom[i];
}
this.length=len;
this.selector=selector || '';
return this;
}
// 生成jquery对象
function Z(elements,selector){
return Query.call(this,elements,selector);
}
// 具体的dom查找
function qsa(element,selector){
return element.querySelectorAll(selector);
}
Z.prototype={
each(callback){
[].every.call(this,function(el,index){
return callback.call(el,index,el) !==false;
})
},
find(selector){
let doms=[];
this.each(function(index,el){
let childs=this.querySelectorAll(selector);
doms.push(...childs);
})
return new Z(doms,selector);
},
eq(i){
let doms=[];
this.each(function(index,el){
if(i==index){
doms.push(this);
}
})
return new Z(doms,this.selector);
},
remove(){
this.each(function(index,el){
this.remove();
})
}
}
// 全局方法
function isFunction(value){
return typeof value==='function';
}
function $(nodeSelector){
let doms=qsa(document,nodeSelector);
return new Z(doms,nodeSelector)
}
$.isFunction=isFunction;
return $;
})(window);
手写jquery代码
最新推荐文章于 2024-04-23 09:52:58 发布