<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
</head
>
<body
>
<input type
="text" id
="numOne" placeholder
="请输入第一个数"/>
<select id
="operation">
<option
>+</option
>
<option
>-</option
>
<option
>*</option
>
<option
>/</option
>
<option
>%</option
>
</select
>
<input type
="text" id
="numTwo" placeholder
="请输入第二个数"/>
<input type
="button" value
="计算" onclick
="result()"/>
<hr
/>
计算结果
:<span id
="result"></span
>
<script
>
function result() {
var numOne
= document
.getElementById("numOne").value
;
var numTwo
= document
.getElementById("numTwo").value
;
var operation
= document
.getElementById("operation").value
;
var re
= /^[0-9]+.?[0-9]*/;
if (re
.test(numOne
) || re
.test(numTwo
)) {
if (operation
== "/") {
if (numTwo
== 0) {
alert("除数不能为0");
} else {
var result
= eval(numOne
+ operation
+ numTwo
);
document
.getElementById("result").innerText
= result
.toFixed(2);
}
} else {
var result
= eval(numOne
+ operation
+ numTwo
);
document
.getElementById("result").innerText
= result
.toFixed(2);
}
} else {
alert("请输入大于零的数字");
}
}
</script
>
</body
>
</html
>
转载请注明原文地址: https://lol.8miu.com/read-9022.html