如何实现撤销功能?js可以吗? 如题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 当然可以,参见:Command模式-命令模式 简单写个参考的:<style type="text/css">div{border:3px solid black;padding:10px;}</style><div id="x">初始值</div><script type="text/javascript">//要应用command模式的对象var box = function(dbox,svalue){ this.value = svalue; this.dbox = dbox; this.restart = function(){//约定一个回到初始状态的方法 this.value = svalue; this.dbox.innerHTML = svalue; };};box.prototype = { setValue:function(svalue){ this.value = svalue; this.dbox.innerHTML = svalue; }};//command对象var command = function(receiver){ this.receiver = receiver; this.log = []; this.un_re_do = -1;};command.prototype = { query:function(cmd){ if(!cmd||cmd.constructor!=Function){return;} var args = Array.prototype.slice.call(arguments,0); var idx = this.un_re_do+1,ll = this.log.length; if(ll){this.log.splice(idx,ll-idx);} this.log.push(args); this.un_re_do++; this.exec.apply(this,args); }, exec:function(){ var args = Array.prototype.slice.call(arguments,0); if(!args.length){return;} var cmd = args.shift(); if(cmd.constructor!=Function){return;} cmd.apply(this.receiver,args); }, undo:function(){ var idx = (this.un_re_do>=0)?--this.un_re_do:-1; this.unredo(idx); }, redo:function(){ var idx = (this.un_re_do<this.log.length-1)?++this.un_re_do:this.un_re_do; this.unredo(idx); }, unredo:function(idx){ if(idx>=0){ var cmd_step = this.log[idx]; this.exec.apply(this,cmd_step); }else{ this.receiver.restart(); } }};var hellobox = new box(document.getElementById('x'),'初始值');var boxCommand = new command(hellobox);window.setTimeout(function(){boxCommand.query(hellobox.setValue,'第一次修改值!');},1000);window.setTimeout(function(){boxCommand.undo();},2000);window.setTimeout(function(){boxCommand.query(hellobox.setValue,'第二次修改值!');},3000);window.setTimeout(function(){boxCommand.undo();},4000);window.setTimeout(function(){boxCommand.redo();},5000);window.setTimeout(function(){boxCommand.query(hellobox.setValue,'第三次修改值!');},6000);window.setTimeout(function(){boxCommand.query(hellobox.setValue,'第四次修改值!');},7000);window.setTimeout(function(){boxCommand.undo();},8000);window.setTimeout(function(){boxCommand.undo();},9000);window.setTimeout(function(){boxCommand.undo();},10000);</script> jquery 用ID取值 undefind 找不到元素 请问在IE上显示正常,在火狐上怎么不显示,该如何修改 关于在线图像处理程序的问题 这问题估计无数人提过,我不知道想问一次 js 问题。。来一下。。。看看谁能解决??? 这是怎么回事?这么简单的程序就是得不到正确结果? frameset的问题 用js如何取得string的右边n个字符? 如何给一个URL进行加密操作,不让客户端看到所传递参数及其值? 如何写一个tree view? 请教高手 Ext 托盘代码 关于ext中拖拽的问题
<style type="text/css">
div{border:3px solid black;padding:10px;}
</style><div id="x">初始值</div><script type="text/javascript">
//要应用command模式的对象
var box = function(dbox,svalue){
this.value = svalue;
this.dbox = dbox;
this.restart = function(){//约定一个回到初始状态的方法
this.value = svalue;
this.dbox.innerHTML = svalue;
};
};
box.prototype = {
setValue:function(svalue){
this.value = svalue;
this.dbox.innerHTML = svalue;
}
};
//command对象
var command = function(receiver){
this.receiver = receiver;
this.log = [];
this.un_re_do = -1;
};
command.prototype = {
query:function(cmd){
if(!cmd||cmd.constructor!=Function){return;}
var args = Array.prototype.slice.call(arguments,0);
var idx = this.un_re_do+1,ll = this.log.length;
if(ll){this.log.splice(idx,ll-idx);}
this.log.push(args);
this.un_re_do++;
this.exec.apply(this,args);
},
exec:function(){
var args = Array.prototype.slice.call(arguments,0);
if(!args.length){return;}
var cmd = args.shift();
if(cmd.constructor!=Function){return;}
cmd.apply(this.receiver,args);
},
undo:function(){
var idx = (this.un_re_do>=0)?--this.un_re_do:-1;
this.unredo(idx);
},
redo:function(){
var idx = (this.un_re_do<this.log.length-1)?++this.un_re_do:this.un_re_do;
this.unredo(idx);
},
unredo:function(idx){
if(idx>=0){
var cmd_step = this.log[idx];
this.exec.apply(this,cmd_step);
}else{
this.receiver.restart();
}
}
};var hellobox = new box(document.getElementById('x'),'初始值');
var boxCommand = new command(hellobox);window.setTimeout(function(){boxCommand.query(hellobox.setValue,'第一次修改值!');},1000);
window.setTimeout(function(){boxCommand.undo();},2000);
window.setTimeout(function(){boxCommand.query(hellobox.setValue,'第二次修改值!');},3000);
window.setTimeout(function(){boxCommand.undo();},4000);
window.setTimeout(function(){boxCommand.redo();},5000);
window.setTimeout(function(){boxCommand.query(hellobox.setValue,'第三次修改值!');},6000);
window.setTimeout(function(){boxCommand.query(hellobox.setValue,'第四次修改值!');},7000);
window.setTimeout(function(){boxCommand.undo();},8000);
window.setTimeout(function(){boxCommand.undo();},9000);
window.setTimeout(function(){boxCommand.undo();},10000);
</script>