Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript类返回数组,创建类似jQuery的库_Javascript_Jquery_Html - Fatal编程技术网

Javascript类返回数组,创建类似jQuery的库

Javascript类返回数组,创建类似jQuery的库,javascript,jquery,html,Javascript,Jquery,Html,我正在创建类似jQuery的javascript库,我已经成功地添加了prototypehtml(),但是如果我使用$(选择器)调用它,它将返回类似{'el':[array]}的对象,如果将函数更改为则返回this.el它返回数组,但我不能调用.html() 它如何在不破坏原型的情况下返回[array] window.$=函数(选择器){ 如果(!(此实例为$){ 返回新的$(选择器); } this.el=[]; this.html=函数(str){ this.el.forEach(函数(e

我正在创建类似jQuery的javascript库,我已经成功地添加了prototype
html()
,但是如果我使用
$(选择器)
调用它,它将返回类似
{'el':[array]}
的对象,如果将函数更改为
则返回this.el
它返回数组,但我不能调用
.html()

它如何在不破坏原型的情况下返回
[array]

window.$=函数(选择器){
如果(!(此实例为$){
返回新的$(选择器);
}
this.el=[];
this.html=函数(str){
this.el.forEach(函数(el){
el.innerHTML=str;
});
};
(功能(自我){
对于(var eList=document.querySelectorAll(选择器),i=eList.length-1;i>-1;i--){
self.el[i]=eList[i];
}
})(本条);
归还这个;
};
$('#test').html('BBBBB')
console.log($('#test')[0])
AAAAAAA
window.$=函数(选择器){
var x=[];
x=document.querySelectorAll(选择器);
控制台日志(x);
返回x;
};
NodeList.prototype.html=函数(str){
console.log(this);
this.forEach(函数(el){
el.innerHTML=str;
});
}
console.log($('#test').html('BBBBB'))

最后,通过查看
Zepto
源代码,将对象转换为数组的神奇之处在于,您必须创建
$.prototype.splice
,它是从
数组中复制的。splice

//仅当页面上没有jQuery时加载
如果(窗口。$==未定义)窗口。$=(函数(){
var$,zepto={},emptyArray=[];
函数Z(dom,选择器){
变量i,len=dom?dom.length:0;
对于(i=0;i
AAAA
BBBB

CCCC
与jQuery类似,如果我调用
$(“#test”)
而不是
$(“#test”).el,它应该返回数组。