网页版计算器

<html>
<head>
<title>js计算器</title>
<meta charset="UTF-8"/>
<!--声明css代码域-->
<style type="text/css">
/*设置div样式*/
#math{
text-align: center;
margin: auto;
margin-top: 100px;
border: solid 1px;
border-radius: 10px;
width: 300px;
height: 400px;
}
/*设置文本框*/
#num{
width: 270px;
height: 40px;
margin-top: 20px;
}
/*设置按钮样式*/
input[type=button]{
border: solid 1px orange;
border-radius: 10px;
width: 60px;
height: 60px;
margin: 5px;
margin-top: 16px;
font-size: 30px;
}
</style>
<!--声明js代码域-->
<script type="text/javascript">
//声明函数
function operNum(num){
//获取文本框
var inp=document.getElementById("num");
//使用switch进行判断
switch (num){
case "=":
inp.value=eval(inp.value);
break;
case "C":
inp.value="";
break;
default:
inp.value=inp.value+num;
break;
}
}
</script>
</head>
<body>
<div id="math">
<input type="text" name="num" id="num" value="" /><br />
<input type="button" id="" value="1"
"operNum(this.value)"/>
<input type="button" id="" value="2"
"operNum(this.value)"/>
<input type="button" id="" value="3"
"operNum(this.value)"/>
<input type="button" id="" value="4"
"operNum(this.value)"/><br />
<input type="button" id="" value="5"
"operNum(this.value)"/>
<input type="button" id="" value="6"
"operNum(this.value)"/>
<input type="button" id="" value="7"
"operNum(this.value)"/>
<input type="button" id="" value="8"
"operNum(this.value)"/><br />
<input type="button" id="" value="9"
"operNum(this.value)"/>
<input type="button" id="" value="0"
"operNum(this.value)"/>
<input type="button" id="" value="+"
"operNum(this.value)"/>
<input type="button" id="" value="-"
"operNum(this.value)"/><br />
<input type="button" id="" value="*"
"operNum(this.value)"/>
<input type="button" id="" value="/"
"operNum(this.value)"/>
<input type="button" id="" value="C"
"operNum(this.value)"/>
<input type="button" id="" value="="
"operNum(this.value)"/>
</div>
</body>
</html>

在这里插入图片描述

好的,以下是一个简单的 HTML 网页版计算器的代码示例: ```html <!DOCTYPE html> <html> <head> <title>网页版计算器</title> <meta charset="UTF-8"> <style> input[type=button] { width: 50px; height: 50px; font-size: 20px; margin: 5px; } input[type=text] { width: 206px; height: 50px; font-size: 20px; margin: 5px; } </style> </head> <body> <h1>网页版计算器</h1> <input type="text" id="result" readonly><br> <input type="button" value="1" onclick="appendValue('1')"> <input type="button" value="2" onclick="appendValue('2')"> <input type="button" value="3" onclick="appendValue('3')"> <input type="button" value="+" onclick="appendValue('+')"><br> <input type="button" value="4" onclick="appendValue('4')"> <input type="button" value="5" onclick="appendValue('5')"> <input type="button" value="6" onclick="appendValue('6')"> <input type="button" value="-" onclick="appendValue('-')"><br> <input type="button" value="7" onclick="appendValue('7')"> <input type="button" value="8" onclick="appendValue('8')"> <input type="button" value="9" onclick="appendValue('9')"> <input type="button" value="*" onclick="appendValue('*')"><br> <input type="button" value="0" onclick="appendValue('0')"> <input type="button" value="." onclick="appendValue('.')"> <input type="button" value="C" onclick="clearResult()"> <input type="button" value="/" onclick="appendValue('/')"><br> <input type="button" value="=" onclick="calculate()"> <script> function appendValue(value) { document.getElementById("result").value += value; } function clearResult() { document.getElementById("result").value = ""; } function calculate() { var result = eval(document.getElementById("result").value); document.getElementById("result").value = result; } </script> </body> </html> ``` 该网页版计算器使用了 HTML、CSS 和 JavaScript 技术,包含一个文本框用于显示运算结果,以及若干个按钮用于输入数字和运算符。JavaScript 代码实现了输入内容的追加、清空、以及运算结果的计算和显示。注意,该计算器仅作为示例,不具备完整的计算器功能和安全性,仅供学习和参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值