加减乘除计算器练习实例
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>加减乘除计算器</title>
<script type="text/javascript">
function count(){
var a = parseInt(document.getElementById("txt1").value);
var b = parseInt(document.getElementById("txt2").value) ;
var sign = document.getElementById("select").value;
var result = 0;
switch(sign){
case "+": result = a+b;
break;
case "-": result = a-b;
break;
case "": result = ab;
break;
case "/": result = a/b;
break;
}
document.getElementById("fruit").value = result;
}
</script>
</head>
<body>
<input type='text' id='txt1' />
<select id='select'>
<option value='+'>+</option>
<option value="-">-</option>
<option value=""></option>
<option value="/">/</option>
</select>
<input type='text' id='txt2' />
<input type='button' value=' = ' onclick="count()"/> <!--通过 = 按钮来调用创建的函数,得到结果-->
<input type='text' id='fruit' />
</body>
</html>
共同学习,写下你的评论
评论加载中...
作者其他优质文章