(function(){
var W=window.W=window.$=function(selector){
return new W.core.init(selector);
};
W.core=W.prototype={
/*selector*/
init:function(els){
var elements=[];
for(var i=0,l=els.length;i<l;i++){
var element=arguments[i];
if(typeof element=='string'){
element=document.getElementById(element);
}
}
if(arguments.length===1){
return element;
}
elements.push(element);
return elements;
},
each:function(fn){
for(var i=0,l=this.elements.length;i<l;i++){
fn.call(this,this.elements[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(){
that.setStyle("display","");
});
return this;
},
hide:function(){
var that=this;
this.each(function(){
that.setStyle("display","none");
});
return this;
}
}
W.core.init.prototype=W.core;
})();
然后调用此js
<script type="text/javascript">
window.onload=function(){
$('p').hide();
}
</script>为何链式调用不起作用?
var W=window.W=window.$=function(selector){
return new W.core.init(selector);
};
W.core=W.prototype={
/*selector*/
init:function(els){
var elements=[];
for(var i=0,l=els.length;i<l;i++){
var element=arguments[i];
if(typeof element=='string'){
element=document.getElementById(element);
}
}
if(arguments.length===1){
return element;
}
elements.push(element);
return elements;
},
each:function(fn){
for(var i=0,l=this.elements.length;i<l;i++){
fn.call(this,this.elements[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(){
that.setStyle("display","");
});
return this;
},
hide:function(){
var that=this;
this.each(function(){
that.setStyle("display","none");
});
return this;
}
}
W.core.init.prototype=W.core;
})();
然后调用此js
<script type="text/javascript">
window.onload=function(){
$('p').hide();
}
</script>为何链式调用不起作用?
解决方案 »
- 求大神帮我解答如下题
- 求一JAVAscript效果,层的显示与隐藏!!!!!!!!!!!!!!
- 为啥FILTER:progid:DXImageTransform.Microsoft.Gradient(非信任站点)无效
- 表格里面的div隐藏后表格的高度随DIV的隐藏而变化
- 弱弱的问一下为什么!
- 求飞入飞出购物车特效
- 请问怎样去除一个字符串的空格
- 计时器的问题!
- 急问为什麽在window2000系统下可以使用的javascrip函数,在windows98就报错,如何修改?
- 我的一个主页面打开很多窗口,怎样在主页面关闭的时候关闭所有子窗体,而且主页面每隔10分钟自动刷新一次。
- JQuery右键菜单的问题???高手帮忙
- 帮我看看,这个问题该怎么解决?关于javascript与ajax
回复内容太短了!
// 让init返回当前实例
init:function(els){
this.elements=[];
for(var i=0,l=els.length;i<l;i++){
var element=arguments[i];
if(typeof element=='string'){
element=document.getElementById(element);
}
}
this.elements.push(element);
return this;
},