js练习计算器
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>图形计算器</title>
<style>
div{
width: 400px;
height: 600px;
border: 1px black solid;
margin: 0 auto;
text-align: center;
}
section{
width: 100px;
height: 50px;
background-color: aqua;
margin: 30px auto;
}
</style>
</head>
<body>
<div>
<section></section>
<input id="width" type="text" placeholder="请输入宽度(cm)"><br>
<input id="height" type="text" placeholder="请输入高度(cm)"><br>
<button onclick="calc()">计算</button>
<p id="result"></p>
</div>
<script>
function calc(){
// 获取宽度和高度
var width = document.getElementById("width").value
var height = document.getElementById("height").value
// 计算周长和面积
var side = width*2 + height*2
var area = width * height
// 显示计算结果
var rsP = document.getElementById("result")
// toFixed(n) 保留小数点后n位
rsP.innerText = "周长:" + side.toFixed(2) + ",面积:" + area.toFixed(2)
}
</script>
</body>
</html>