首页 > 代码库 > 用javascript加减乘除

用javascript加减乘除

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>

</style>
</head>
<body>
<div>
<input type="text" size="10" id="num1" />
<select name="sel" id="sel">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
<option>%</option>
</select>
<input type="text" size="10" id="num2" />
<button onclick="myadd()">=</button>
<input type="text" size="10" id="num3" readonly />
<div id="div11" style="color: red; font-size: 12px;" >

</div>
</div>
<script>
function myadd(){
var div11 = document.getElementById(‘div11‘);
var num1 = document.getElementById(‘num1‘);
var num2 = document.getElementById(‘num2‘);
var num3 = document.getElementById(‘num3‘);
var sel = document.getElementById(‘sel‘);

div11.innerHTML = "";
if(num1.valuehttp://www.mamicode.com/=="" || isNaN(num1.value) || num2.valuehttp://www.mamicode.com/=="" || isNaN(num2.value)){
div11.innerHTML = ‘输入的内容不是数字‘;
}else{
if(sel.value=http://www.mamicode.com/=‘+‘){
num3.value = http://www.mamicode.com/(parseFloat(num1.value) + parseFloat(num2.value)).toFixed(2);
}else if(sel.value=http://www.mamicode.com/=‘-‘){
num3.value = http://www.mamicode.com/(parseFloat(num1.value) - parseFloat(num2.value)).toFixed(2);
}else if(sel.value=http://www.mamicode.com/=‘*‘){
num3.value = http://www.mamicode.com/(parseFloat(num1.value) * parseFloat(num2.value)).toFixed(2);
}else if(sel.value=http://www.mamicode.com/=‘/‘){
if(parseFloat(num2.value)==0){
div11.innerHTML = ‘被除数不能为0‘;
}else{
num3.value = http://www.mamicode.com/(parseFloat(num1.value) / parseFloat(num2.value)).toFixed(2);
}
}else if(sel.value=http://www.mamicode.com/=‘%‘){
if(parseFloat(num2.value)==0){
div11.innerHTML = ‘被除数不能为0‘;
}else{
num3.value = http://www.mamicode.com/(parseFloat(num1.value) % parseFloat(num2.value)).toFixed(2);
}
}
}
}
</script>
</body>
</html>

用javascript加减乘除