我试图用javascript制作这个简单的计算器。我已经检查了类的名称和所有的名称,但我找不到任何问题。除了控制台也没有显示任何问题,但是当我单击按钮时仍然没有任何反应。这是html文件:
<!DOCTYPE html>
<html dir="ltr">
<head>
<meta charset="utf-8">
<meta name="viewport">
<link rel="stylesheet" href="style.css">
<title>Calculator</title>
</head>
<body>
<section class="calculator">
<form>
<div class="previous_op"></div>
<div class="current_op"></div>
</form>
<button class ='clearBtn span-two'>AC</button>
<button class ='delBtn'>DEL</button>
<button class='btn btn-orange' data-num='/'>/</button>
<button class='btn btn-grey' data-num='1'>1</button>
<button class='btn btn-grey' data-num='2'>2</button>
<button class='btn btn-grey' data-num='3'>3</button>
<button class='btn btn-orange' data-num='*'>*</button>
<button class='btn btn-grey' data-num='4'>4</button>
<button class='btn btn-grey' data-num='5'>5</button>
<button class='btn btn-grey' data-num='6'>6</button>
<button class='btn btn-orange' data-num='+'>+</button>
<button class='btn btn-grey' data-num='7'>7</button>
<button class='btn btn-grey' data-num='8'>8</button>
<button class='btn btn-grey' data-num='9'>9</button>
<button class='btn btn-orange' data-num='-'>-</button>
<button class='btn btn-grey' data-num='.'>.</button>
<button class='btn btn-grey' data-num='0'>0</button>
<button class ='span-two equalBtn'>=</button>
</section>
</body>
<script src="javascript.js"></script>
</html>
这是javascript文件:
const btns = document.querySelector('.btn');
const prevOp = document.querySelector('.previous_op');
const currOp = document.querySelector('.current_op');
const equalBtn = document.querySelector('.equalBtn');
const clearBtn = document.querySelector('.clearBtn');
const deleteBtn = document.querySelector('.delBtn');
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener('click', function() {
let number= btns[i].getAttribute('data-num');
currOp.value += number;
})
}
蝴蝶刀刀
相关分类