fn.call(this,this.elements[i]); //..虽然看了call的手册,这里还是难于理解而且这段代码中为什么还要这样写call呢?上面代码是不是相当于:function(el){el.style[prop] = val;}.call(this,this.elements[i]);谁详细的讲解一下这个的执行流程?
<div id="box">mybox</div>
(function(){
function _$(els){
this.elements = [];
for(var i=0, len = els.length; i<len; i++){
var element = els[i];
if(typeof element === "string"){
element = document.getElementById(element);
}
this.elements.push(element);
}
}
_$.prototype = {
each : function(fn){
for(var i=0, len = this.elements.length; i<len; i++){
fn.call(this,this.elements[i]);
console.log(this.elements[i])
}
},
setStyle : function(prop,val){
this.each(function(el){
console.log(el)
el.style[prop] = val;
})
}
}
window.$ = function(){
return new _$(arguments);
}
})();
$("box").setStyle("color","red")
2 回答
添加回答
举报
0/150
提交
取消