js或jquery 可以做到这些吗? 想在一个网页的文本框里实现类似Excel单元格那种输入数字和运算符即能计算的功能(=12+21-10 然后回车即计算出:23)规则是:文本框里先输等号,然后是数字,接着是运算符,然后再是运算符,即:=12+5+56-5…… 回车后得出结果放在文本框里。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以的,加个事件,如onkeydown,然后检查是否按下回车,如果是,检查内容,是否以=开头,是就eval一下就OK了 <!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></head><body><textarea rows="3" cols="20" id="calculator" style="background-color:#66FF99;"></textarea><script>var cal = document.getElementById('calculator');cal.onkeydown = function(e){ e = e || event; if(this.style.backgroundColor == '#f00')this.style.backgroundColor = '#66FF99'; if(e.keyCode == 13){ if(/^\=[\d,\+,\-,\*,\/,\(,\)]*$/.test(this.value)){ this.value = eval(this.value.replace('=','')); }else{ this.style.backgroundColor = '#f00'; } return false; }}</script></body></html>只给你做了加减乘除。没做容错处理。大体就这么个意思 <script>var cal = document.getElementById('calculator');cal.onkeydown = function(e){ e = e || event; if(this.style.backgroundColor == '#f00')this.style.backgroundColor = '#66FF99'; if(e.keyCode == 13){ if(/^[\d,\.,\+,\-,\*,\/,\(,\)]*\=$/.test(this.value)){ this.value = eval(this.value.replace('=','')); }else{ this.style.backgroundColor = '#f00'; } return false; }}</script> ext datefield 格式的转换。 很诡异的JS代码,弹窗COOKIE控制,绝对高效的代码 ext 新手提问? 如何动态的改变Html中tr行的背景颜色 在jsp中如何加闪烁的标签!!! 有将字符串进行GBK Encode的函数没? 如何写正则表达式让字符串中不能有2a3d几个字符中的任意一个 可输入的select改进版本,同一页面可有多个list,调用接口简化 问梅花雨 ☆☆☆☆谁有向资源管理器一样的树型结构脚本程序。在线等候。 javascript prototype constructor 求解 【分享】这几天学jquery,刚写了个可编辑表格,求批评……
<!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>
</head><body>
<textarea rows="3" cols="20" id="calculator" style="background-color:#66FF99;"></textarea>
<script>
var cal = document.getElementById('calculator');
cal.onkeydown = function(e){
e = e || event;
if(this.style.backgroundColor == '#f00')this.style.backgroundColor = '#66FF99';
if(e.keyCode == 13){
if(/^\=[\d,\+,\-,\*,\/,\(,\)]*$/.test(this.value)){
this.value = eval(this.value.replace('=',''));
}else{
this.style.backgroundColor = '#f00';
}
return false;
}
}
</script>
</body>
</html>只给你做了加减乘除。
没做容错处理。
大体就这么个意思
<script>
var cal = document.getElementById('calculator');
cal.onkeydown = function(e){
e = e || event;
if(this.style.backgroundColor == '#f00')this.style.backgroundColor = '#66FF99';
if(e.keyCode == 13){
if(/^[\d,\.,\+,\-,\*,\/,\(,\)]*\=$/.test(this.value)){
this.value = eval(this.value.replace('=',''));
}else{
this.style.backgroundColor = '#f00';
}
return false;
}
}
</script>