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> ExtJs的root.reload()问题 进来看看吧,字符串遍历问题 使用JavaScript调用XFire发布的webservice遇到的问题 JS函数调用 求一个纯JS的web编辑器? 一个字符串拼接问题 如何屏蔽在页面上点击右键时弹出的弹出式菜单! 请教:比如在一道题中四个单选框(radio),那么怎么判断多个题目中的单选框被选中呢 !制作最差网页的秘诀! 高分求助在线wait...... 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>