计算器

2019-06-14  本文已影响0人  多喝烫水_

<!DOCTYPE html>

<html>

<head>

<title>计算器</title>

<script type="text/javascript">

window.onload = function(){

var txt1 = document.getElementById("txt1");

var txt2 = document.getElementById("txt2");

var select1 = document.getElementById("select");

var btn = document.getElementById("btn");

var btn1 = document.getElementById("btn1");

btn.onclick = function(){

var val1 = txt1.value;

var val2 = txt2.value;

if (val1=="" || val2=="") {

alert("输入的内容不能为空!请重新输入:");

return;

}

if (isNaN(val1) || isNaN(val2)) {

alert("请输入数字!");

return;

}

switch(select1.value){

case "0":

alert((parseFloat(val1)*100 + parseFloat(val2)*100)/100)

break

case "1":

alert((parseFloat(val1)*100 - parseFloat(val2)*100)/100)

break

case "2":

alert((parseFloat(val1)*100 * parseFloat(val2)*100)/10000)

break

case "3":

alert((parseFloat(val1)*100 / parseFloat(val2)*100)/100)

break

}

}

btn1.onclick = function(){

txt1.value = "";

txt2.value = "";

}

}

</script>

</head>

<body>

<h1>计算器</h1>

<input type="text" name="" id="txt1">

<select id="select">

<option value="0">+</option>

<option value="1">-</option>

<option value="2">*</option>

<option value="3">/</option>

</select>

<input type="text" name="" id="txt2">

<input type="button" name="" value="计算" id="btn">

<input type="button" name="" value="清除" id="btn1">

</body>

</html>

上一篇 下一篇

猜你喜欢

热点阅读