gpt4 book ai didi

javascript - HTML 计算器不会显示任何内容

转载 作者:太空宇宙 更新时间:2023-11-04 07:21:14 24 4
gpt4 key购买 nike

我的 HTML 计算器不显示输入或结果。如果可能的话,我想保留我的布局和设计。我只用 HTML 代码制作了这个计算器。我希望在现有网站上进行访问是基本的。我试过搞乱 JavaScript 和 jQuery,但我对它们的理解还不够好,无法让它运行。我也尝试了一些调试软件,但他们还没有缩小问题的范围。

HTML 计算器代码:

body {
background-color: #FFFFFF;
background-size: 1500px;
}

#calculator {
width: 250px;
height: 375px;
text-align: center;
margin: 90px auto;
box-shadow: 10px 10px 10px #010012;
background-color: #229EE2;
background-size: 7px;
border-radius: 30px;
border: 3px solid #595959;
}

#display {
margin: 30px 0 20px 0;
padding-right: 2%;
width: 220px;
height: 30px;
border-radius: 4px;
box-shadow: 0px 0px 3px #595959;
text-align: right;
font: 27px bold;
color: #1A3C67;
background-color: #78c4ed;
}

#keys {
width: 43px;
height: 35px;
margin-left: 10px;
margin-bottom: 20px;
box-shadow: 0px 0px 15px #010012;
cursor: pointer;
font-style: italic;
color: lightblue;
background-color: rgb(0, 50, 68);
font-weight: bold;
font-size: 24px;
border-radius: 4px;
}

#keys:hover {
background: #1998cd;
font-weight: bold;
color: #1e185e;
}

#keysC {
width: 43px;
height: 35px;
margin-left: 10px;
margin-bottom: 20px;
box-shadow: 0px 0px 10px #595959;
cursor: pointer;
font-style: italic;
color: #1A3C67;
background-color: lightblue;
font-weight: bold;
font-size: 16px;
border-radius: 4px;
}

#keysC:hover {
background: #7caad0;
font-weight: bold;
color: #1A3C67;
}
<html>

<head>
<title>Financial Calculator</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
<div id="calculator">
<div class="fluid-container">
<form name="Financial Calculator">
<input type="textfield" id="display" name="ans" value="" class="form-control" placeholder="">
<br>
<div class="row">
<div>
<input type="reset" id="keysC" value="C">
<input type="reset" id="keysC" value="CE">
<input type="button" id="keysC" value="<--">
<input type="button" id="keysC" value="%" onClick="document.calculator.ans.value+='%'">
</div>
</div>
<br>
<input type="button" id="keys" value="7" onClick="document.calculator.ans.value+='7'">
<input type="button" id="keys" value="8" onClick="document.calculator.ans.value+='8'">
<input type="button" id="keys" value="9" onClick="document.calculator.ans.value+='9'">
<input type="button" id="keysC" value="/" onClick="document.calculator.ans.value+='/'">
<br>
<input type="button" id="keys" value="4" onClick="document.calculator.ans.value+='4'">
<input type="button" id="keys" value="5" onClick="document.calculator.ans.value+='5'">
<input type="button" id="keys" value="6" onClick="document.calculator.ans.value+='6'">
<input type="button" id="keysC" value="*" onClick="document.calculator.ans.value+='*'">
<br>
<input type="button" id="keys" value="1" onClick="document.calculator.ans.value+='1'">
<input type="button" id="keys" value="2" onClick="document.calculator.ans.value+='2'">
<input type="button" id="keys" value="3" onClick="document.calculator.ans.value+='3'">
<input type="button" id="keysC" value="-" onClick="document.calculator.ans.value+='-'">
<br>
<input type="button" id="keys" value="0" onClick="document.calculator.ans.value+='0'">
<input type="button" id="keys" value="." onClick="document.calculator.ans.value+='.'">
<input type="button" id="keys" value="=" onClick="document.calculator.ans.value=eval(document.calculator.ans.value)">
<input type="button" id="keysC" value="+" onClick="document.calculator.ans.value+='+'">
</form>
</div>
</div>
</body>

</html>

最佳答案

首先,你写的是onClick,但实际上是onclick,因为那里区分大小写

其次,您编写了 document.calculator.ans.value不起作用。相反,您只需要 ans.value 就可以了。

下面我包含了计算器的 HTML 代码以显示您想要的内容(有效)

body {
background-color: #FFFFFF;
background-size: 1500px;
}

#calculator {
width: 250px;
height: 375px;
text-align: center;
margin: 90px auto;
box-shadow: 10px 10px 10px #010012;
background-color: #229EE2;
background-size: 7px;
border-radius: 30px;
border: 3px solid #595959;
}

#display {
margin: 30px 0 20px 0;
padding-right: 2%;
width: 220px;
height: 30px;
border-radius: 4px;
box-shadow: 0px 0px 3px #595959;
text-align: right;
font: 27px bold;
color: #1A3C67;
background-color: #78c4ed;
}

#keys {
width: 43px;
height: 35px;
margin-left: 10px;
margin-bottom: 20px;
box-shadow: 0px 0px 15px #010012;
cursor: pointer;
font-style: italic;
color: lightblue;
background-color: rgb(0, 50, 68);
font-weight: bold;
font-size: 24px;
border-radius: 4px;
}

#keys:hover {
background: #1998cd;
font-weight: bold;
color: #1e185e;
}

#keysC {
width: 43px;
height: 35px;
margin-left: 10px;
margin-bottom: 20px;
box-shadow: 0px 0px 10px #595959;
cursor: pointer;
font-style: italic;
color: #1A3C67;
background-color: lightblue;
font-weight: bold;
font-size: 16px;
border-radius: 4px;
}

#keysC:hover {
background: #7caad0;
font-weight: bold;
color: #1A3C67;
}
    <!DOCTYPE html>
<html>

<head>
<title>Financial Calculator</title>
<link rel="stylesheet" type="text/css" href="style123.css">
</head>

<body>
<div id="calculator">
<div class="fluid-container">
<form name="Financial Calculator">
<input type="text" id="display" name="ans" value="" class="form-control" placeholder="">
<br>
<div class="row">
<div>
<input type="reset" id="keysC" value="C">
<input type="reset" id="keysC" value="CE">
<input type="button" id="keysC" value="<--">
<input type="button" id="keysC" value="%" onclick="ans.value+='%'">
</div>
</div>
<br>
<input type="button" id="keys" value="7" onclick="ans.value+='7'">
<input type="button" id="keys" value="8" onclick="ans.value+='8'">
<input type="button" id="keys" value="9" onclick="ans.value+='9'">
<input type="button" id="keysC" value="/" onclick="ans.value+='/'">
<br>
<input type="button" id="keys" value="4" onclick="ans.value+='4'">
<input type="button" id="keys" value="5" onclick="ans.value+='5'">
<input type="button" id="keys" value="6" onclick="ans.value+='6'">
<input type="button" id="keysC" value="*" onclick="ans.value+='*'">
<br>
<input type="button" id="keys" value="1" onclick="ans.value+='1'">
<input type="button" id="keys" value="2" onclick="ans.value+='2'">
<input type="button" id="keys" value="3" onclick="ans.value+='3'">
<input type="button" id="keysC" value="-" onclick="ans.value+='-'">
<br>
<input type="button" id="keys" value="0" onclick="ans.value+='0'">
<input type="button" id="keys" value="." onclick="ans.value+='.'">
<input type="button" id="keys" value="=" onclick="ans.value=eval(ans.value)">
<input type="button" id="keysC" value="+" onclick="ans.value+='+'">
</form>
</div>
</div>
</body>

</html>

关于javascript - HTML 计算器不会显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50262479/

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