closest()
选择的第一个元素调用querySelectorAll
到底有什么问题? var $ = function(selector){
var x;
var obj = {
myLib(selector){
return x || document.querySelectorAll(selector);
},
cl(selector){
x[0].closest(selector);
return this;
},
style(aaa,bbb){
!aaa && !bbb ? x.getAttribute('style') :
aaa && bbb ? x.forEach( zzz => { zzz.style[aaa]=bbb; } ) :
aaa.includes(';') ? x.forEach( zzz => { zzz.style.cssText+=aaa; } )
: getComputedStyle(x,null)[aaa];
return this;
},
};
x = obj.myLib(selector);
return obj;
};
// now i want to call it
$('div').style('background','#FFFF00');
$('.kilo').cl('.uniform').style('color:','#880088');
<div id="foxtrott">
foxtrott
<div class="uniform">
uniform
<div class="charlie">
charlie
<div class="kilo">
kilo
</div>
</div>
</div>
</div>
当我将cl()函数更改为此时,控制台会告诉我:$(...).cl(...) is null
cl(selector){
return x[0].closest(selector);
},
我真的不知道如何正确地做到这一点。 :(
我尝试在我的自定义JavaScript库中对由querySelectorAll选择的第一个元素调用closest()到底有什么问题? var $ = function(selector){var x; var obj = {...
x
中设置cl
。