日期:2014-05-16  浏览次数:20325 次

关于删除最后一个字符串的问题。求帮助
我现在 有三个button 按钮上分别显示 "我的" "名字" "是zjx"

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

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

------解决方案--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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">
var a=[];
function change(x){
document.getElementById('test').innerHTML+=x;
a.push(x);
}
function change2(){
var div=document.getElementById('test');
var t=div.innerHTML;
var p=a.pop();
if(p){
var end=t.lastIndexOf(p);
t=t.substring(0,end);
div.innerHTML=t;
}
}
</script>
</head>

<body>
<div id="test"></div>
<input type="button" value="我的" onclick="change('我的')">
<input type="button" value="名字" onclick="change('名字')">
<input type="button" value="是zyx" onclick="change('是zyx')">
<input type="button" value="删除上一步" onclick="change2()">
</body>
</html>
类似这样试试
------解决方案--------------------
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<div id='show'></div>

<button id='b1'>my</button>
<button id='b2'>name</button>
<button id='b3'>is zjx</button>
<button id='clear'>clear one</button>
<script type="text/javascript">
function $(id){
return document.getElementById(id);
}
function init(){
var sets=[];
function b1fn(){
if(!b1fn.clicked){
sets.push('my');
//b1fn.clicked=true;
$('show').innerHTML=sets.join('');
}
}
function b2fn(){
if(!b2fn.clicked){
sets.push(' name');
//b2fn.clicked=true;
$('show').innerHTML=sets.join('');
}
}
function b3fn(){
if(!b3fn.clicked){
sets.push(' is zjx');
//b3fn.clicked=true;
$('show').innerHTML=sets.join('');
}
}
function clear(){
sets.pop();
$('show').innerHTML=sets.join('');
}


$('b1').addEventListener('click',b1fn);
$('b2').addEventListener('click',b2fn);
$('b3').addEventListener('click',b3fn);
$('clear').addEventListener('click',clear);
}
init();

</script>

</body>
</html>

------解决方案--------------------
按楼上朋友的思路可以解决你的问题.可以修改成这样:
function change2(){
    a.pop();
    document.getElementById('test').innerHTML=a.join('');
}