亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

JS實現(xiàn)簡單的計算器功能

asal 2019-02-20 09:55:09 1112
abstrak:<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>計算器</title> <style> *{padding: 0px;margin: 0px;} div{margin: 10
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>計算器</title>
<style>
*{padding: 0px;margin: 0px;}
div{margin: 10px;}
input{width: 150px;height: 30px;border: 1px solid #ccc;text-align: center;}
select{
width: 38px;height: 30px;border: 1px solid #ccc;
}
button{
width: 80px;height: 32px;border: 1px solid #ccc;
}
</style>
</head>
<body>
<div>
<input type="number" name="one">
<select name="fuhao">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
<option value="%">%</option>
</select>
<input type="number" name="two">
=
<input type="text" name="three">
<button onclick="jisuan()">計算</button>
</div>
</body>
<script type="text/javascript">
function jisuan(){
var one_list = document.getElementsByName('one');
var two_list = document.getElementsByName('two');
var fuhao_list = document.getElementsByName('fuhao');
var three_list = document.getElementsByName('three');
var one_value = parseFloat(one_list[0].value);
var two_value = parseFloat(two_list[0].value);
var fuhao_value = fuhao_list[0].value;
//獲取運算符
switch(fuhao_value){
case '+': 
three_list[0].value = one_value + two_value;
break;
case '-': 
three_list[0].value = one_value - two_value;
break;
case '*': 
three_list[0].value = one_value * two_value;
break;
case '/': 
three_list[0].value = one_value / two_value;
break;
case '%': 
three_list[0].value = one_value % two_value;
break;
}
// alert(one_value);
console.log(fuhao_value);
}

</script>
</html>

執(zhí)行代碼如下:

QQ截圖20190220095304.jpg

Guru membetulkan:天蓬老師Masa pembetulan:2019-02-20 10:20:14
Rumusan guru:parseFloat(one_list[0].value);這行代碼是關(guān)鍵, 將字符型的數(shù)字轉(zhuǎn)換成數(shù)值型, 才能進行計算, 不要用自動轉(zhuǎn)換

Nota Keluaran

Penyertaan Popular