<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>计算器online</title>
</head>
<body>
<input type="text" id="txt1" />
<select id="sel">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" id="txt2" />
=
<input type="text" id="txt3" />
<!-- 方法1 -->
<!-- <input type="button" id="btn" value="计算" οnclick="test()"/> -->
<!-- 方法2 -->
<input type="button" id="btn" value="计算" />
</body>
<script type="text/javascript">
//方法1 给按钮写了一个点击事件,然后通过调用test()函数实现所需要的效果
/*function test(){
var txt1 = document.getElementById("txt1"),
txt2 = document.getElementById("txt2"),
txt3 = document.getElementById("txt3"),
opt = document.getElementById("sel");
txt3.value = eval(txt1.value + opt.value + txt2.value);//eval函数可计算某个字符串,并执行其中的的js代码
}*/
//方法2 更加简便一点,直接给按钮写个点击事件的函数,一点击按钮就直接执行下面的操作,比上面的更加好理解一些
var txt1 = document.getElementById("txt1"),
txt2 = document.getElementById("txt2"),
txt3 = document.getElementById("txt3"),
opt = document.getElementById("sel");
btn.onclick = function(){
txt3.value = eval(txt1.value + opt.value + txt2.value);
}
</script>
<html>
<head>
<meta charset="utf-8">
<title>计算器online</title>
</head>
<body>
<input type="text" id="txt1" />
<select id="sel">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" id="txt2" />
=
<input type="text" id="txt3" />
<!-- 方法1 -->
<!-- <input type="button" id="btn" value="计算" οnclick="test()"/> -->
<!-- 方法2 -->
<input type="button" id="btn" value="计算" />
</body>
<script type="text/javascript">
//方法1 给按钮写了一个点击事件,然后通过调用test()函数实现所需要的效果
/*function test(){
var txt1 = document.getElementById("txt1"),
txt2 = document.getElementById("txt2"),
txt3 = document.getElementById("txt3"),
opt = document.getElementById("sel");
txt3.value = eval(txt1.value + opt.value + txt2.value);//eval函数可计算某个字符串,并执行其中的的js代码
}*/
//方法2 更加简便一点,直接给按钮写个点击事件的函数,一点击按钮就直接执行下面的操作,比上面的更加好理解一些
var txt1 = document.getElementById("txt1"),
txt2 = document.getElementById("txt2"),
txt3 = document.getElementById("txt3"),
opt = document.getElementById("sel");
btn.onclick = function(){
txt3.value = eval(txt1.value + opt.value + txt2.value);
}
</script>
</html>
以上便是我写的一个简易计算器,希望多多少少能给大家带来些许的帮助