首页 > 代码库 > 用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加减乘除
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。