gpt4 book ai didi

javascript - 使用Javascript的计算器

转载 作者:行者123 更新时间:2023-12-04 08:34:50 25 4
gpt4 key购买 nike

我正在尝试使用 Vanilla Javascript 制作一个简单的计算器。有一个div有一类main-content,其下有一个ID为displaytext的text-box .此外,有 3 行,每行包含数字,然后是另一行包含符号。我附上了下面的代码以供引用。

const displayText = document.getElementById("displaytext"); // storing the ID of the textbox in a variable
const seven = document.getElementById("seven");

seven.addEventListener('click',function () {
displayText.value+=seven.value;
})
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Open+Sans&display=swap');


*{
margin: 0;
padding: 0;
box-sizing: border-box;
}

/* .main-content{
display: flex;
justify-content: center;
align-items: center;
} */

.heading{
margin: 20px;
padding:15px;
}

.heading h2{
text-align: center;
font-family: 'Open Sans', sans-serif;
font-size: 45px;
font-weight: 800;
margin-left: -20px;
}

.main-content{
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 75vh;
background-color: rgb(255, 49, 49,0.5);
width: 30%;
margin: 0px 520px;
border: 3px solid rgb(197, 95, 0);
border-radius: 30px;
}

.row1,.row2,.row3,.row4{
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
/* margin: 10px; */
padding: 10px;
margin-left: -70px;
}

.element{
padding:25px;
background-color: white;
text-align: center;
margin: 7.5px;

font-size: 25px;
font-family: 'Roboto', sans-serif;
border: 4px solid rgb(102, 102, 102);
border-radius: 15px;
cursor: pointer;
width: 50%;

}

.symbol{
display: flex;
flex-direction: column;
/* align-items: flex-end; */
/* justify-content: right; */
margin-top: -355px;
margin-left: 300px;
}

.symbol-element{
padding: 17.5px;
margin: 10px 0px;
border: 4px solid rgb(102, 102, 102);
font-size: 25px;
font-family: 'Roboto', sans-serif;
border-radius: 15px;
cursor: pointer;
width: 120%;
text-align: center;
height: 66px;
/* transition: 0.5s ease-in-out; */
}

.displaytext{
border-radius: 10px;
/* margin-left: 5px; */
height: 100px;
width: 75%;
padding: 20px;
/* margin: 20px 0px; */
margin-left: 35px;
margin-top: 20px;
border: none;
font-size: 17px;
/* transition: 5s ease-in-out; */
}

.element,.symbol-element,.displaytext:focus{
outline: none;
}
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>

<link rel="stylesheet" href="/calculator/style.css">
</head>

<body>

<!-- main-container starts here -->

<div class="main-container">

<div class="heading">

<h2>Calculator</h2>

</div>

<form class="main-content">

<input type="text" class="displaytext" name="displaytext" id="displaytext">

<div class="row1">
<button class="element" value="7" id="seven">7</button>
<button class="element" value="8">8</button>
<button class="element" value="9">9</button>

</div>

<div class="row2">
<button class="element" value="1">1</button>
<button class="element" value="2">2</button>
<button class="element" value="3">3</button>

</div>

<div class="row3">
<button class="element" value="4">4</button>
<button class="element" value="5">5</button>
<button class="element" value="6">6</button>

</div>

<div class="symbol">
<button class="symbol-element" value="+">+</button>
<button class="symbol-element" value="-">-</button>
<button class="symbol-element" value="*">*</button>
<button class="symbol-element" value="/">/</button>

</div>






<!-- main-content ends here -->




</div>

<!-- outer div ends here -->

<script src="/calculator/script.js"></script>

</body>

</html>

这里的问题是,每当我运行命令 displayText.value+=seven.value; ,该值仅在文本框中显示一秒钟。我该如何解决相同的问题。

最佳答案

button s 是表单元素和它们的默认值 typesubmit , 添加 type="button"属性到按钮,所以他们不发送表单。

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button

The HTML <button> element represents a clickable button, used to submit forms or anywhere in a document for accessible, standard button functionality. By default, HTML buttons are presented in a style resembling the platform the user agent runs on, but you can change buttons’ appearance with CSS.



const displayText = document.getElementById("displaytext"); // storing the ID of the textbox in a variable
const seven = document.getElementById("seven");

seven.addEventListener('click',function () {
displayText.value+=seven.value;
})
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Open+Sans&display=swap');


*{
margin: 0;
padding: 0;
box-sizing: border-box;
}

/* .main-content{
display: flex;
justify-content: center;
align-items: center;
} */

.heading{
margin: 20px;
padding:15px;
}

.heading h2{
text-align: center;
font-family: 'Open Sans', sans-serif;
font-size: 45px;
font-weight: 800;
margin-left: -20px;
}

.main-content{
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 75vh;
background-color: rgb(255, 49, 49,0.5);
width: 30%;
margin: 0px 520px;
border: 3px solid rgb(197, 95, 0);
border-radius: 30px;
}

.row1,.row2,.row3,.row4{
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
/* margin: 10px; */
padding: 10px;
margin-left: -70px;
}

.element{
padding:25px;
background-color: white;
text-align: center;
margin: 7.5px;

font-size: 25px;
font-family: 'Roboto', sans-serif;
border: 4px solid rgb(102, 102, 102);
border-radius: 15px;
cursor: pointer;
width: 50%;

}

.symbol{
display: flex;
flex-direction: column;
/* align-items: flex-end; */
/* justify-content: right; */
margin-top: -355px;
margin-left: 300px;
}

.symbol-element{
padding: 17.5px;
margin: 10px 0px;
border: 4px solid rgb(102, 102, 102);
font-size: 25px;
font-family: 'Roboto', sans-serif;
border-radius: 15px;
cursor: pointer;
width: 120%;
text-align: center;
height: 66px;
/* transition: 0.5s ease-in-out; */
}

.displaytext{
border-radius: 10px;
/* margin-left: 5px; */
height: 100px;
width: 75%;
padding: 20px;
/* margin: 20px 0px; */
margin-left: 35px;
margin-top: 20px;
border: none;
font-size: 17px;
/* transition: 5s ease-in-out; */
}

.element,.symbol-element,.displaytext:focus{
outline: none;
}
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>

<link rel="stylesheet" href="/calculator/style.css">
</head>

<body>

<!-- main-container starts here -->

<div class="main-container">

<div class="heading">

<h2>Calculator</h2>

</div>

<form class="main-content">

<input type="text" class="displaytext" name="displaytext" id="displaytext">

<div class="row1">
<button class="element" value="7" id="seven" type="button">7</button>
<button class="element" value="8">8</button>
<button class="element" value="9">9</button>

</div>

<div class="row2">
<button class="element" value="1">1</button>
<button class="element" value="2">2</button>
<button class="element" value="3">3</button>

</div>

<div class="row3">
<button class="element" value="4">4</button>
<button class="element" value="5">5</button>
<button class="element" value="6">6</button>

</div>

<div class="symbol">
<button class="symbol-element" value="+">+</button>
<button class="symbol-element" value="-">-</button>
<button class="symbol-element" value="*">*</button>
<button class="symbol-element" value="/">/</button>

</div>






<!-- main-content ends here -->




</div>

<!-- outer div ends here -->

<script src="/calculator/script.js"></script>

</body>

</html>

您还可以同时处理循环上的点击功能和属性类型

let displayText = document.getElementById("displaytext"); // storing the ID of the textbox in a variable
let btn = document.querySelectorAll(".element, .symbol-element"); // find all our calculator buttons
for (let i = 0; i < btn.length; i++) {// loop through btn found
btn[i].setAttribute("type", "button"); // remove the submit behavior
btn[i].addEventListener("click", function() {
displayText.value += this.value; // add to display text the button value
});
}
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Open+Sans&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

.heading {
margin: 20px;
padding: 15px;
}

.heading h2 {
text-align: center;
font-family: 'Open Sans', sans-serif;
font-size: 45px;
font-weight: 800;
margin-left: -20px;
}

.main-content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 75vh;
background-color: rgb(255, 49, 49, 0.5);
width: 30%;
margin: 0px 520px;
border: 3px solid rgb(197, 95, 0);
border-radius: 30px;
}

.row1,
.row2,
.row3,
.row4 {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
/* margin: 10px; */
padding: 10px;
margin-left: -70px;
}

.element {
padding: 25px;
background-color: white;
text-align: center;
margin: 7.5px;
font-size: 25px;
font-family: 'Roboto', sans-serif;
border: 4px solid rgb(102, 102, 102);
border-radius: 15px;
cursor: pointer;
width: 50%;
}

.symbol {
display: flex;
flex-direction: column;
/* align-items: flex-end; */
/* justify-content: right; */
margin-top: -355px;
margin-left: 300px;
}

.symbol-element {
padding: 17.5px;
margin: 10px 0px;
border: 4px solid rgb(102, 102, 102);
font-size: 25px;
font-family: 'Roboto', sans-serif;
border-radius: 15px;
cursor: pointer;
width: 120%;
text-align: center;
height: 66px;
/* transition: 0.5s ease-in-out; */
}

.displaytext {
border-radius: 10px;
/* margin-left: 5px; */
height: 100px;
width: 75%;
padding: 20px;
/* margin: 20px 0px; */
margin-left: 35px;
margin-top: 20px;
border: none;
font-size: 17px;
/* transition: 5s ease-in-out; */
}

.element,
.symbol-element,
.displaytext:focus {
outline: none;
}
<!-- main-container starts here -->

<div class="main-container">
<div class="heading">
<h2>Calculator</h2>
</div>
<form class="main-content">
<input type="text" class="displaytext" name="displaytext" id="displaytext">
<div class="row1">
<button class="element" value="7">7</button>
<button class="element" value="8">8</button>
<button class="element" value="9">9</button>
</div>

<div class="row2">
<button class="element" value="1">1</button>
<button class="element" value="2">2</button>
<button class="element" value="3">3</button>
</div>

<div class="row3">
<button class="element" value="4">4</button>
<button class="element" value="5">5</button>
<button class="element" value="6">6</button>
</div>

<div class="symbol">
<button class="symbol-element" value="+">+</button>
<button class="symbol-element" value="-">-</button>
<button class="symbol-element" value="*">*</button>
<button class="symbol-element" value="/">/</button>
</div>

<!-- main-content ends here -->
</form>

<!-- outer div ends here -->
</div>

关于javascript - 使用Javascript的计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64844798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com