我正试图制作一个计算器,但JavaScript文件显示了一个错误。
var top = document.getElementById('top');
var bottom = document.getElementById('bottom');
function calculator(value) {
if (value === 'AC') {
top.innerText = (top.innerText).slice(0, -1);
} else if (value === '=') {
top.innerText = ''
} else {
top.innerText += value;
}
bottom.innerText = `${eval(((top.innerText)).replace('%', '/100').replace('x', '*'))}`;
if (bottom.innerText === 'undefined') {
bottom.innerText = 0;
}
}
button {
border-radius: 10px;
margin-top: 5px;
font-size: 200%;
width: 6%;
background: #F7ECDE;
border: 2px solid black;
box-shadow: 0 2px #999;
cursor: pointer;
}
button:active {
box-shadow: 0 3px #666;
transform: translateY(2px);
}
button:hover {
background-color: rgba(0, 0, 0, 0.709);
color: rgb(215, 254, 255);
transition: background-color .5s;
}
.container {
text-align: center;
}
.display {
height: 75px;
margin: auto;
width: 24.6%;
border: 2px solid black;
}
.top,
.bottom {
background: rgb(219, 204, 204);
padding-right: 5px;
text-align: right;
}
.top {
font-size: 100%;
height: 25px;
}
.bottom {
font-size: 200%;
height: 50px;
border-top: 1px solid black;
}
@media (max-width:950px) {
button {
font-size: 100%;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="display">
<div id="top" class="top"></div>
<div id="bottom" class="bottom"></div>
</div>
<div><button onclick="calculator(this.innerText)">(</button>
<button onclick="calculator(this.innerText)">)</button>
<button onclick="calculator(this.innerText)">%</button>
<button ondblclick="Top.innerText=''" class="AC" onclick="calculator(this.innerText)">AC</button>
</div>
<div>
<button onclick="calculator(this.innerText)">7</button>
<button onclick="calculator(this.innerText)">8</button>
<button onclick="calculator(this.innerText)">9</button>
<button on onclick="calculator(this.innerText)">/</button>
</div>
<div>
<button onclick="calculator(this.innerText)">4</button>
<button onclick="calculator(this.innerText)">5</button>
<button onclick="calculator(this.innerText)">6</button>
<button onclick="calculator(this.innerText)">x</button>
</div>
<div>
<button onclick="calculator(this.innerText)">1</button>
<button onclick="calculator(this.innerText)">2</button>
<button onclick="calculator(this.innerText)">3</button>
<button onclick="calculator(this.innerText)">-</button>
</div>
<div>
<button onclick="calculator(this.innerText)" class="zero">0</button>
<button onclick="calculator(this.innerText)">.</button>
<button onclick="calculator(this.innerText)">=</button>
<button onclick="calculator(this.innerText)">+</button>
</div>
</div>
<script src="index.js"></script>
</body>
</html>
当我使用此代码时会显示错误。但是,如果我将更改变量名top
更改为JavaScript文件中的任何其他名称,它将非常有效。
var Top = document.getElementById('top');
var bottom = document.getElementById('bottom');
function calculator(value) {
if (value === 'AC') {
Top.innerText = (Top.innerText).slice(0, -1);
} else if (value === '=') {
Top.innerText = ''
} else {
Top.innerText += value;
}
bottom.innerText = `${eval(((Top.innerText)).replace('%', '/100').replace('x', '*'))}`;
if (bottom.innerText === 'undefined') {
bottom.innerText = 0;
}
}
button {
border-radius: 10px;
margin-top: 5px;
font-size: 200%;
width: 6%;
background: #F7ECDE;
border: 2px solid black;
box-shadow: 0 2px #999;
cursor: pointer;
}
button:active {
box-shadow: 0 3px #666;
transform: translateY(2px);
}
button:hover {
background-color: rgba(0, 0, 0, 0.709);
color: rgb(215, 254, 255);
transition: background-color .5s;
}
.container {
text-align: center;
}
.display {
height: 75px;
margin: auto;
width: 24.6%;
border: 2px solid black;
}
.top,
.bottom {
background: rgb(219, 204, 204);
padding-right: 5px;
text-align: right;
}
.top {
font-size: 100%;
height: 25px;
}
.bottom {
font-size: 200%;
height: 50px;
border-top: 1px solid black;
}
@media (max-width:950px) {
button {
font-size: 100%;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="/css/style.css">
<script defer src="/js/index.js"></script>
</head>
<body>
<div class="container">
<div class="display">
<div id="top" class="top"></div>
<div id="bottom" class="bottom"></div>
</div>
<div><button onclick="calculator(this.innerText)">(</button>
<button onclick="calculator(this.innerText)">)</button>
<button onclick="calculator(this.innerText)">%</button>
<button ondblclick="Top.innerText=''" class="AC" onclick="calculator(this.innerText)">AC</button>
</div>
<div>
<button onclick="calculator(this.innerText)">7</button>
<button onclick="calculator(this.innerText)">8</button>
<button onclick="calculator(this.innerText)">9</button>
<button on onclick="calculator(this.innerText)">/</button>
</div>
<div>
<button onclick="calculator(this.innerText)">4</button>
<button onclick="calculator(this.innerText)">5</button>
<button onclick="calculator(this.innerText)">6</button>
<button onclick="calculator(this.innerText)">x</button>
</div>
<div>
<button onclick="calculator(this.innerText)">1</button>
<button onclick="calculator(this.innerText)">2</button>
<button onclick="calculator(this.innerText)">3</button>
<button onclick="calculator(this.innerText)">-</button>
</div>
<div>
<button onclick="calculator(this.innerText)" class="zero">0</button>
<button onclick="calculator(this.innerText)">.</button>
<button onclick="calculator(this.innerText)">=</button>
<button onclick="calculator(this.innerText)">+</button>
</div>
</div>
</body>
</html>
为什么会发生这种情况?
这显示了一个错误,因为top返回窗口对象层次结构中最顶层的窗口。如果执行console.log(top)
,则会发现window object
而不是HtmlElement。