js小效果-简易计算器

时间:2022-06-18 15:54:44
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<script>
window.onload=function(){
var oTxt1=document.getElementById('txt1');
var oTxt2=document.getElementById('txt2');
var oSl=document.getElementById('sl');
var oBtn=document.getElementById('btn');
oBtn.onclick=function(){
var a=oTxt1.value;
var b=oTxt2.value;
alert(eval(a+oSl.value+b));
}
}
</script>
</head>
<body>
<input type="text" name="" id="txt1"/>
<select name="" id="sl">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
<option value="%">%</option>
</select>
<input type="text" name="" id="txt2"/>
<input type="button" name="" id="btn" value="按钮"/>
</body>
</html>