您的位置:首页 > 其它

计算器的退格键点击实现删除字符文字

2017-06-30 17:17 267 查看
一、首先写个例子,点击退格实现删除文本框的字符文字:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>退格删除</title>
</head>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script type="text/javascript">
function del(){
var s2 = $("#test").val();
s2 = s2.substring("",s2.length-1);
s2 = (s2 == "" ? "" : s2);
$("#test").val(s2);
}
</script>;

<body>
<input type="text" id="test" />
<input type="button" onclick="del()" value="退格" />
</body>
</html>



二、计算器的退格键点击实现删除字符文字:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>退格删除</title>
<style>
.myinput{ width:70px; height:30px;}
.tf{ width:220px; height:30px; margin-bottom:5px; font-size:26px; font-family:Tahoma, Geneva, sans-serif; color:#fff; border:2px solid #999; background:#000; text-align:right;}
}
</style>
</head>
<script type="text/javascript">
window.onload = function(){
var tf = $("tf");
for( var i=0;i<11;i++ ){
$("btn"+i).onclick = function(){
if(this.value == "." && tf.value == "") return false;
if(this.value == "." && tf.value.indexOf(".") != -1) return false;
if(tf.value == "0"){
if(this.value == "."){
tf.value += this.value;
}
}else{
tf.value += this.value;
}
}
}
$("back").onclick = function(){
tf.value = tf.value.replace(/.$/,'')
}
}
function $(id){return document.getElementById(id);}
</script>

<body>
<input class="tf" name="textfield" type="text" id="tf" size="30" />
<br />
<input class="myinput" type="submit" name="button" id="btn0" value="0" />
<input class="myinput" type="submit" name="button" id="btn1" value="1" />
<input class="myinput" type="submit" name="button" id="btn2" value="2" />
<br />
<input class="myinput" type="submit" name="button" id="btn3" value="3" />
<input class="myinput" type="submit" name="button" id="btn4" value="4" />
<input class="myinput" type="submit" name="button" id="btn5" value="5" />
<br />
<input class="myinput" type="submit" name="button" id="btn6" value="6" />
<input class="myinput" type="submit" name="button" id="btn7" value="7" />
<input class="myinput" type="submit" name="button" id="btn8" value="8" />
<br />
<input class="myinput" type="submit" name="button" id="btn9" value="9" />
<input class="myinput" type="submit" name="button" id="btn10" value="." />
<input class="myinput" type="submit" name="button" id="back" value="退格" />
</body>
</html>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: