1 <script type="text/javascript"> 2 var arrayObj = new Array(); 3 var lastStr; 4 $(document).ready(function () { 5 $(".textBotton").click(function () { 6 lastStr = $(this).val(); 7 arrayObj.push(lastStr); 8 $("#showDiv").html($("#showDiv").html() + lastStr); 9 }); 10 $("#delLastStr").click(function () { 11 if (arrayObj.length > 0) { 12 var strShow = $("#showDiv").html().toString(); 13 lastStr = arrayObj[arrayObj.length-1]; 14 var index = strShow.lastIndexOf(lastStr); 15 if (index != -1 && index + lastStr.length == strShow.length) { 16 strShow = strShow.toString().substring(0, strShow.lastIndexOf(lastStr)); 17 $("#showDiv").html(strShow); 18 } 19 arrayObj.pop(); 20 } 21 }); 22 }); 23 </script>
1 <div id="showDiv"></div> 2 <input type="button" class="textBotton" value="我的" /> 3 <input type="button" class="textBotton" value="名字" /> 4 <input type="button" class="textBotton" value="是zjx" /> 5 <input type="button" id="delLastStr" value="删除上一步" />
thank you 解决了
在你的“历史”数据中,只需要保存动作序列,一个LinkedList,就足够了。
谢谢 提供思路 已经解决了
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script type="text/javascript" language="javascript"> var history = { index: 0 , Steps: [{ result: "", buttons: [] }] , OnClick: function (button) { if (history.index < history.Steps.length - 1) { history.Steps.splice(history.index+1, history.Steps.length - 1 - history.index); } var flag = false; var s = ""; var state = history.Steps[history.index]; var buttons = []; for (var i = 0; i < state.buttons.length; i++) { if (state.buttons[i] == button) { state.buttons.slice(i, 1); flag = true; continue; } else { buttons.push(state.buttons[i]); } s += state.buttons[i].value; } if (!flag) { buttons.push(button); s += button.value; } history.Steps.push({ result: s, buttons: buttons }); history.index++; history.UpdateButtonEnabled(); document.getElementById("response").innerHTML = s; } , Status: [] , Undo: function () { if (history.index > 0) { history.index--; } history.UpdateButtonEnabled(); document.getElementById("response").innerHTML = history.Steps[history.index].result; } , Redo: function () { if (history.index < history.Steps.length - 1) { history.index++; } history.UpdateButtonEnabled(); document.getElementById("response").innerHTML = history.Steps[history.index].result; } , UpdateButtonEnabled: function () { if (history.index < 1) { //Undo Disabled document.getElementById("btnUndo").setAttribute("disabled", "disabled"); } else { //Undo Enabled document.getElementById("btnUndo").removeAttribute("disabled"); } if (history.index < history.Steps.length - 1) { //Redo Enabled document.getElementById("btnRedo").removeAttribute("disabled"); } else { //Redo Disabled document.getElementById("btnRedo").setAttribute("disabled", "disabled"); } } }; </script> </head> <body> <div id="container"> <input type="button" value="1" onclick="history.OnClick(this)" /> <input type="button" value="2" onclick="history.OnClick(this)" /> <input type="button" value="3" onclick="history.OnClick(this)" /> <input type="button" value="4" onclick="history.OnClick(this)" /> <input type="button" value="5" onclick="history.OnClick(this)" /> <input type="button" value="6" onclick="history.OnClick(this)" /> <input type="button" value="7" onclick="history.OnClick(this)" /> <input type="button" value="8" onclick="history.OnClick(this)" /> </div> <div id="response"></div> <div> <input type="button" value="Undo" id="btnUndo" onclick="history.Undo()" disabled="disabled" /> <input type="button" value="Redo" id="btnRedo" onclick="history.Redo()" disabled="disabled" /> </div> </body> </html>
看到题目就写测试代码去了,回来发现楼上已经给出答案了,不过还是把代码贴出来吧,支持任意按钮数量、支持任意步骤的撤销重做。代码如下:
thank you 解决了
想复杂了楼主.
过程如下.
按钮点击一次,加入一个javascript的栈里面,然后将栈里面的数据,打印到div里面
后退时.
删除栈顶元素,打印到div里面
谢谢提供思路 已经解决了