首页 新闻 会员 周边

关于删除最后一个字符串的问题。求帮助

0
悬赏园豆:20 [已解决问题] 解决于 2014-02-13 22:10

我现在 有三个button 按钮上分别显示 "我的" "名字" "是zjx"

三个按钮的功能是将其按钮上的内容分别添加到一个dvi中并显示 例如依次点击按钮 div中内容就为”我的名字是zjx“

现在我想要设置一个 按钮 为"删除上一步" 其作用是 删除上一次点击的按钮的内容 如最后点击的是"名字" 则把"名字" 字符串删除 这个怎么实现

青空下的思念的主页 青空下的思念 | 初学一级 | 园豆:4
提问于:2014-02-12 21:59
< >
分享
最佳答案
0
 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="删除上一步" />
收获园豆:5
yyutudou | 小虾三级 |园豆:997 | 2014-02-12 22:52

thank  you 解决了

青空下的思念 | 园豆:4 (初学一级) | 2014-02-13 22:14
其他回答(3)
0

在你的“历史”数据中,只需要保存动作序列,一个LinkedList,就足够了。

收获园豆:5
雾静 | 园豆:561 (小虾三级) | 2014-02-12 22:45

谢谢 提供思路 已经解决了

支持(0) 反对(0) 青空下的思念 | 园豆:4 (初学一级) | 2014-02-13 22:15
0
<!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>

 

看到题目就写测试代码去了,回来发现楼上已经给出答案了,不过还是把代码贴出来吧,支持任意按钮数量、支持任意步骤的撤销重做。代码如下:

收获园豆:5
I,Robot | 园豆:9783 (大侠五级) | 2014-02-12 23:43

thank you 解决了

支持(0) 反对(0) 青空下的思念 | 园豆:4 (初学一级) | 2014-02-13 22:15
0

想复杂了楼主.

过程如下.

按钮点击一次,加入一个javascript的栈里面,然后将栈里面的数据,打印到div里面

后退时.

删除栈顶元素,打印到div里面

收获园豆:5
迅捷网络[来送福利] | 园豆:576 (小虾三级) | 2014-02-13 13:19

 谢谢提供思路 已经解决了

支持(0) 反对(0) 青空下的思念 | 园豆:4 (初学一级) | 2014-02-13 22:15
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册