html+js实现简单的计算器代码(加减乘除)
投稿:jingxian
下面小编就为大家带来一篇html+js实现简单的计算器代码(加减乘除)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
html+js实现简单的计算器代码(加减乘除)
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <table> <tr> <td><input type="button" value="add" onclick="setOp('+', 'add');"/></td> <td><input type="button" value="miner" onclick="setOp('-', 'miner');"/></td> <td><input type="button" value="times" onclick="setOp('*', 'times');"/></td> <td><input type="button" value="divide" onclick="setOp('/', 'divide');"/></td> </tr> </table> <table id="tb_calc" style="display:none;"> <tr> <td> <input id="x" type="text" style="width:100px" value="" name="x" /></td> <td> <lable id="op" name="op"></lable> </td> <td> <input id="y" type="text" style="width:100px" value="" name="y" /> </td> <td> <input id="opTips" type="button" value="" onclick="calc();"/> </td> <td> <lable id="z" name="z"></lable> </td> </tr> </table> <script type="application/javascript"> function setOp(op, opTips) { var tb=document.getElementById("tb_calc"); tb.style.display = "none"; document.getElementById("x").value = ""; document.getElementById("y").value = ""; document.getElementById("z").innerText = ""; document.getElementById("op").innerText = op; document.getElementById("opTips").value = opTips; tb.style.display = "block"; } function calc() { var x = parseInt(document.getElementById("x").value); var y = parseInt(document.getElementById("y").value); var op = document.getElementById("op").innerText; var z = ""; switch(op) { case '+': z = x + y; break; case '-': z = x - y; break; case '*': ; z = x * y; break; case '/': ; z = x / y; break; default: z = ''; } console.log(x, op, y, '=', z); document.getElementById("z").innerText = z; } </script> </body> </html>
截图如下:
以上这篇html+js实现简单的计算器代码(加减乘除)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。