簡簡單單,仔細一點,答案,僅供參考!
<!DOCTYPE html>
<html>
?<head>
? <title>事件</title> ?
? <script type="text/javascript">
? ?function count(){
? ? ? ?//第1個輸入框
? ? ? ?var num1 = document.getElementById("txt1").value;
? ? ? ?//運算符
? ? ? ?var ysf = document.getElementById("select").value;
? ? ? ?//第2個輸入框
? ? ? ?var num2 = document.getElementById("txt2").value;
? ? ? ?//結(jié)果框
? ? ? ?var fruit = document.getElementById("fruit");
? ? ? switch(ysf){
? ? ? ? ? case "+":
? ? ? ? ? ? fruit.value = parseInt(num1)+parseInt(num2);
? ? ? ? ? break;
? ? ? ? ? ? case "-":
? ? ? ? ? ? fruit.value = parseInt(num1)-parseInt(num2);
? ? ? ? ? break;
? ? ? ? ? ? case "*":
? ? ? ? ? ? fruit.value = parseInt(num1)*parseInt(num2);
? ? ? ? ? break;
? ? ? ? ? ? case "/":
? ? ? ? ? ? fruit.value = parseInt(num1)/parseInt(num2);
? ? ? ? ? break;
? ? ? }
? ? ? ?
? ?}
? </script>?
?</head>?
?<body>
? ?<input type='text' id='txt1' />?
? ?<select id='select'>
<option value='+'>+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
? ?</select>
? ?<input type='text' id='txt2' />?
? ? <!--通過 = 按鈕來調(diào)用創(chuàng)建的函數(shù),得到結(jié)果-->?
? ?<input type='button' value=' = ' onclick="count()" />
? ?<input type='text' id='fruit' /> ??
?</body>
</html>?
2023-04-20
好厲害啊