3 回答

TA贡献1773条经验 获得超3个赞
实际上我的目标是按输入栏在图像之间切换
let hits = 0;
const hitElement = document.getElementById("hits");
const images = [
"https://via.placeholder.com/50x50.png?text=qwe", // your 1 image
"https://via.placeholder.com/50x50.png?text=rty", // your 2 image
"https://via.placeholder.com/50x50.png?text=uyi", // your 3 image
"https://via.placeholder.com/50x50.png?text=opd", // your 4 image
"https://via.placeholder.com/50x50.png?text=asd", // your 5 image
];
document.body.onkeyup = function (e) {
if (e.keyCode === 32) {
hits++;
hitElement.src = images[hits % images.length];
}
};
<p>Press spacebar</p>
<img id="hits" src="https://via.placeholder.com/50x50.png?text=qwe" />

TA贡献1854条经验 获得超8个赞
<body>
<h1>HELLO WORLD</h1>
<p id="hits" value="0"> 0</p>
<script>
var hits = 0;
var hitElement = document.getElementById("hits");
document.body.onkeyup = function(e) {
if (e.keyCode == 32) {addHit()}}
var addHit = function() {hits++; renderHits()}
var renderHits = function() {hitElement.innerText = hits % 5}
var resetHits = function() {renderHits()}

TA贡献1818条经验 获得超11个赞
它是“点击率 % 5”的东西。解释器将其视为 ((lresult = a) % b) 而不是 (lresult=(a % b))。
需要括号。
let hits = 0;
const hitElement = document.getElementById("hits");
document.body.onkeyup = function (e) {
if (e.keyCode === 32) {
hits++;
hitElement.src = `https://via.placeholder.com/50x50.png?text=${(hits % 5) + 1}`;
}
};
<p>Press spacebar</p>
<img id="hits" src="https://via.placeholder.com/50x50.png?text=1"></ing>
添加回答
举报