1.一个简单的计算器,用JS书写

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>

<script>

onload = function () {

var num1 = document.getElementById ("tx1"); // 声明保存第一个输入框

var num2 = document.getElementById ("tx2"); // 声明保存第二个输入框


var num3 = document.getElementById ("jia"); // 声明一个变量保存加运算

var num4 = document.getElementById ("jian"); // 声明一个变量保存加运算

var num5 = document.getElementById ("chen"); // 声明一个变量保存加运算

var num6 = document.getElementById ("chu"); // 声明一个变量保存加运算


// 一个加法的运算,需要一个触发事件,取浮点数可以运算小数

num3.onclick = function () {

document.write( parseFloat(num1.value) + parseFloat(num2.value) );

}


// 一个减法的运算,需要一个触发事件,取浮点数可以运算小数

num4.onclick = function () {

document.write( parseFloat(num1.value)- parseFloat(num2.value) );

}


// 一个乘法的运算,需要一个触发事件,取浮点数可以运算小数

num5.onclick = function () {

document.write( parseFloat(num1.value) * parseFloat(num2.value) );

}


// 一个除法的运算,需要一个触发事件,取浮点数可以运算小数

num6.onclick = function () {

document.write( parseFloat(num1.value) / parseFloat(num2.value) );

}

}

</script>

<style>

// 以下是一些样式

.box{
height: 200px;
width: 200px;
background: #ccc;
position:fixed;
left: 0;
top: 0;
bottom: 0;
right: 0;
margin: auto;
}
.box input:nth-child(1){
width: 80px;
height: 40px;
border: 1px solid black;
margin-left: 15px;
margin-top: 10px;
}
.box input:nth-child(2){
width:80px;
height: 40px;
border: 1px solid black;
margin-top: 10px;
}
.box input:nth-child(3){
text-align: center;
width: 40px;
height: 40px;
background: black;
color: white;
font-size: 18px;
border: 0;
margin-top: 60px;
margin-left: 4px;
border-radius: 50%;
}
.box input:nth-child(4){
text-align: center;
width: 40px;
height: 40px;
background: black;
color: white;
font-size: 18px;
border: 0;
border-radius: 50%;
}
.box input:nth-child(5){
text-align: center;
width: 40px;
height: 40px;
background: black;
color: white;
font-size: 18px;
border: 0;
border-radius: 50%;
}
.box input:nth-child(6){
text-align: center;
width: 40px;
height: 40px;
background: black;
color: white;
font-size: 18px;
border: 0;
border-radius: 50%;
}

</style>

</head>
<body>


<div class="box">
<input type="text" id="tx1" value=""/>
<input type="text" id="tx2" value=""/>
<input type="button" id="jia" value="+"/>
<input type="button" id="chen" value="*"/>
<input type="button" id="chu" value="/"/>
<input type="button"  id="jian" value="-"/>
</div>


 
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值