使用JavaScript 编写简单计算器
作者:bea
本文方法超级简单,思路非常的值得推荐,小伙伴们参考下吧 代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>javascript 简单计算器</title> <script> //定义全
本文方法超级简单,思路非常的值得推荐,小伙伴们参考下吧
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>javascript 简单计算器</title>
<script>
//定义全局变量 var one="1";
var two="2";
var three="3";
var four="4";
var five="5";
var six="6";
var seven="7";
var eight="8";
var nine="9";
var zero="0";
var plus="+",minus="-",multiply="*",divide="/",decimal=".";
function enter(obj,string){
obj.expr.value+=string;
}
function compute(obj){//单击等于"="按钮调用的函数; obj.expr.value=eval(obj.expr.value);//用JS得eval()方法计算name为expr中的公式 }
</script>
</head>
<body>
<form name="calc" id="calc" action="">
<table border="1">
<td colspan="4"><input type="text" name="expr" size="30" action="compute(this.form)" /></td>
<tr>
<td><input type="button" value=" 7 " onclick="enter(this.form,seven)" /></td>
<td><input type="button" value=" 8 " onclick="enter(this.form,eight)" /></td>
<td><input type="button" value=" 9 " onclick="enter(this.form,nine)" /></td>
<td><input type="button" value=" / " onclick="enter(this.form,divide)" /></td>
</tr>
<tr>
<td><input type="button" value=" 4 " onclick="enter(this.form,four)" /></td>
<td><input type="button" value=" 5 " onclick="enter(this.form,five)" /></td>
<td><input type="button" value=" 6 " onclick="enter(this.form,six)" /></td>
<td><input type="button" value=" * " onclick="enter(this.form,multiply)" /></td>
</tr>
<tr>
<td><input type="button" value=" 1 " onclick="enter(this.form,one)" /></td>
<td><input type="button" value=" 2 " onclick="enter(this.form,two)" /></td>
<td><input type="button" value=" 3 " onclick="enter(this.form,three)" /></td>
<td><input type="button" value=" - " onclick="enter(this.form,minus)" /></td>
</tr>
<tr>
<td colspan="2"><input type="button" value=" 0 " onclick="enter(this.form,zero)" /></td>
<td><input type="button" value=" . " onclick="enter(this.form,decimal)" /></td>
<td><input type="button" value=" + " onclick="enter(this.form,plus)" /></td>
</tr>
<tr>
<td colspan="2"><input type="button" value=" = " onclick="compute(this.form)" /></td>
<td colspan="2"><input type="button" value="AC" onclick="form.reset()"/></td>
</tr>
</table>
</form>
</body>
</html>
是不是超级NB的代码,大神级的!
有用 | 无用
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>javascript 简单计算器</title>
<script>
//定义全局变量 var one="1";
var two="2";
var three="3";
var four="4";
var five="5";
var six="6";
var seven="7";
var eight="8";
var nine="9";
var zero="0";
var plus="+",minus="-",multiply="*",divide="/",decimal=".";
function enter(obj,string){
obj.expr.value+=string;
}
function compute(obj){//单击等于"="按钮调用的函数; obj.expr.value=eval(obj.expr.value);//用JS得eval()方法计算name为expr中的公式 }
</script>
</head>
<body>
<form name="calc" id="calc" action="">
<table border="1">
<td colspan="4"><input type="text" name="expr" size="30" action="compute(this.form)" /></td>
<tr>
<td><input type="button" value=" 7 " onclick="enter(this.form,seven)" /></td>
<td><input type="button" value=" 8 " onclick="enter(this.form,eight)" /></td>
<td><input type="button" value=" 9 " onclick="enter(this.form,nine)" /></td>
<td><input type="button" value=" / " onclick="enter(this.form,divide)" /></td>
</tr>
<tr>
<td><input type="button" value=" 4 " onclick="enter(this.form,four)" /></td>
<td><input type="button" value=" 5 " onclick="enter(this.form,five)" /></td>
<td><input type="button" value=" 6 " onclick="enter(this.form,six)" /></td>
<td><input type="button" value=" * " onclick="enter(this.form,multiply)" /></td>
</tr>
<tr>
<td><input type="button" value=" 1 " onclick="enter(this.form,one)" /></td>
<td><input type="button" value=" 2 " onclick="enter(this.form,two)" /></td>
<td><input type="button" value=" 3 " onclick="enter(this.form,three)" /></td>
<td><input type="button" value=" - " onclick="enter(this.form,minus)" /></td>
</tr>
<tr>
<td colspan="2"><input type="button" value=" 0 " onclick="enter(this.form,zero)" /></td>
<td><input type="button" value=" . " onclick="enter(this.form,decimal)" /></td>
<td><input type="button" value=" + " onclick="enter(this.form,plus)" /></td>
</tr>
<tr>
<td colspan="2"><input type="button" value=" = " onclick="compute(this.form)" /></td>
<td colspan="2"><input type="button" value="AC" onclick="form.reset()"/></td>
</tr>
</table>
</form>
</body>
</html>
是不是超级NB的代码,大神级的!
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- node+express+ejs制作简单页面上手指南
- node.js使用require()函数加载模块
- javascript中setTimeout和setInterval的unref()和ref()用法示例
- 浅谈jQuery中对象遍历.eq().first().last().slice()方法
- javascript 原型链维护和继承详解
- jquery提示效果实例分析
- jQuery操作cookie方法实例教程
- JavaScript常用验证函数实例汇总
- JavaScript导出Excel实例详解
- JS倒计时代码汇总
- jquery中push()的用法(数组添加元素)
- Jquery焦点图实例代码
- jQuery事件绑定和委托实例
- 2014最热门的JavaScript代码高亮插件推荐
- JS跨域问题详解
- javascript 中__proto__和prototype详解
- js 加密压缩出现bug解决方案
- js Object2String方便查看js对象内容
- js的[defer]和[async]属性