| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <input type="text" id="num1">
- <select id="operator">
- <option value="+">+</option>
- <option value="-">-</option>
- <option value="*">*</option>
- <option value="/">/</option>
- </select>
- <input type="text" id="num2">
- <button onclick="calc()">=</button>
- <span id="result"></span>
- </body>
- <script>
- function calc(){
- // 获取输入框内容以后
- var num1 = parseInt(document.getElementById("num1").value)
- var num2 = parseInt(document.getElementById("num2").value)
- // 获取下拉框的值
- var operator = document.getElementById("operator").value
- var ret = 0;
- if(operator == "+"){
- ret = num1+num2
- }else if(operator == "-"){
- ret = num1-num2
- }else if(operator == "*"){
- ret = num1*num2
- }else if(operator == "/"){
- ret = num1/num2
- }
- // 把ret 设置结果div 里面
- document.getElementById("result").innerText = ret
- }
- </script>
- </html>
|