我正在尝试使用 JavaScript 和 jQuery 制作一个内存游戏,现在我想禁止单击在单击时打开的图像(卡片)。我试过img.onclick=false
、img.onclick=null
,但都不起作用。下面是我的代码的一部分。
$(document).ready(function() {
$("img").click(function() {
checkIfMatch(this);
});
});
function checkIfMatch(img) {
arrayIMG.push(img);
img.onclick = false; //here I want to disable the clicking
}
最佳答案
最简单的调整是仅将图像推送到数组中(如果数组中没有):
function checkIfMatch(img) {
if (!arrayIMG.includes(img)) {
arrayIMG.push(img);
// do other stuff when an image is clicked, if any
}
}
如果您在单击新图像时不需要做其他事情,则可以使用 Set 代替(它会自动压缩重复项)使事情变得更容易:
const arrayIMG = new Set();
function checkIfMatch(img) {
arrayIMG.add(img);
}
关于javascript - 如何禁止点击图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63649153/