<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>3.迷你计算器</title>
<style type="text/css">
.box{
width: 500px;
height:200px;
background-color: #efefef;
border: 1px solid lightgray;
text-align: center;
margin: auto;
margin: 20px auto;
color: #636363;
border-radius: 15px;
box-shadow: 2px 2px 2px #999;
}
table {
margin: auto;
}
td {
width: 100px;
height: 100px
padding: 5px 10px;
}
input, select {
width: 100%;
height: 100%;
border: none;
text-align: center;
}
button {
width: 100%;
height: 100%;
border: none;
background-color: skyblue;
color: white;
}
button:hover {
cursor: pointer;
background-color: coral;
width: 105%;
height: 105%;
}
</style>
</head>
<body>
<div>
<h2>迷你计算器</h2>
<form>
<table>
<tr>
<td><input type"text" name="opt1" placeholder="操作数1"></td>
<td>
<select name="option">
<option value="null"> + </option>
<option value="null"> _ </option>
<option value="null"> * </option>
<option value="null"> / </option>
</select>
</td>
<td><input tyep="text" name="opt2" placeholder="操作数2"></td>
<td><button type="button">计算</button></td>
</tr>
<tr>
<td colspan="2"><h3>结果:</h3></td>
<td colspan="2"><h3 id="placeholder"></h3></td>
</tr>
</table>
</form>
</div>
<script type="text/javascript">
var opt1 = document.getElementsByName('opt1')[0]
var opt2 = document.getElementsByName('opt2')[0]
var opt = document.getElementsByName('option')[0]
var btn = document.getElementsByName('button')[0]
var placeholder = document.getElementById('placeholder')
btn.onclick = function(){
if (opt1.value.length == 0) {
alert('第一个操作数不能为空')
opt1.focus()
return false
} else if (isNal(opt1.value)) {
alert('第二个操作数必须为数字')
opt1.focus()
return false()
}
else if (opt2.value.length == 0) {
alert('第三个操作数不能数字')
opt2.focus()
return false()
} else {
var data1 = parseFloat(opt1.value)
var data2 = parseFloat(opt2.value)
}
var option = opt.value
var temp = 0
var flag = ''
var result = ''
switch (option) {
case 'null':
alert("")
opt.focus()
return false
case 'add':
falg = '+'
temp =data1 + data2
break
case 'sub':
flag ='-'
temp = data1 - data2
break
case 'mul':
flag = '*'
temp = data1 * data2
break
case 'div'
flag ='/'
if (data2 == 0) {
alert('除数不能为0,请重新输入')
opt2.focus()
return false
} else {
temp = data1 / data2
}
break
}
var str = '<span style="color:coral">'
str += data1+''+flag+''+data2+'='+temp
str += '</span>'
placeholder.innerHTML = str
}
</script>
</body>
</html>