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>
添加回答
舉報