[code="java"]
function invoice(st,ed,len){
this.spd=20;
this.ul=document.getElementById('invoiceing');
this.dv=this.ul.parentNode;
this.st=parseInt(st,10);
this.ed=parseInt(ed,10);
this.len=len;
this.doing.apply(this);
}
invoice.prototype={
show:function(){
this.ul.innerHTML='';
this.ul.previousSibling.innerHTML='入票中......';
this.dv.style.display='block';
this.dv.style.height=document.body.clientHeight;
this.dv.style.width=document.body.clientWidth;
},
stop:function(){
window.clearInterval(this.running);
this.ul.previousSibling.innerHTML='确定';
this.ul.previousSibling.firstChild.onclick=this.hide;
},
hide:function(){
this.dv.style.height='0px';
this.dv.style.width='0px';
this.dv.style.display='none';
},
rupiao:function(){
var o=this;
return function(){
var li=document.createElement('
[/code]
这个函数在运行完毕时要给"确定"那个超链加上invioce.hide事件 ,但是发现此时的this已经不是invoice了 取不到变量dv
求解
这是因为你赋给dom对象的事件上,问题就在于事件不会马上触发,
等它正确触发的时候,this已经不存在了,this只在对象的方法内有效
this.ul.previousSibling.innerHTML='确定';
var obj = this;
this.ul.previousSibling.firstChild.onclick=obj.hide;
对了,忘了写闭包了
this.ul.previousSibling.innerHTML='确定';
var obj = this;
this.ul.previousSibling.firstChild.onclick= function (){obj.hide()};
这样应该可以了
[quote]# return function(){
hide的被调用者的作用域是o.stop所在的作用域,即rupiao:function()的作用域。
所以可以使用o.stop.call(o);来代替o.stop(); 。那么可以将o的作用域作用于hide方法之上.