关联图像与数组元素结果
我正在使用简单的纸牌游戏来锻炼自己的技能。我设法通过阵列绘制了随机化和循环的卡。我的问题是。现在,我如何将每张卡与图像相关联,以将其渲染到DOM中而无需进行53个否则如果语句呢?有一个简单的方法吗? 这是代码:
<!DOCTYPE html>
add("card");
cardRender.innerHTML = topCard;
//If else argument to tell the computer when the deck is finished!
if(topCard === undefined){
document.getElementById("carpetId").innerHTML = "The deck is EMPTY! Reshuffle it.";
}else{
document.getElementById("carpetId").appendChild(cardRender);
}
const delFirstEl = cardDeck.shift();
return false;
}
</script>
<script>
/*$(document).ready(function(){
$("#btnTest").click(function(){
$("#cardDrawerBox").animate({left: '200px'});
});
});*/
$("#cardDeckId").click(function(){
$('#cardDrawerBox').animate({
top: '300px',
}, 500, function() {
$('#cardDrawerBox').removeAttr('style');
});
});
</script>
</body>
</html>
I'm exercising my skills with a simple card game. I've managed to draw a card randomizing and looping through arrays. My question is. How could I now associate each card to an image for it to be rendered into the Dom without having to make 53 else if statements? Is there an easier way?
Here is the code:
<!DOCTYPE html>
add("card");
cardRender.innerHTML = topCard;
//If else argument to tell the computer when the deck is finished!
if(topCard === undefined){
document.getElementById("carpetId").innerHTML = "The deck is EMPTY! Reshuffle it.";
}else{
document.getElementById("carpetId").appendChild(cardRender);
}
const delFirstEl = cardDeck.shift();
return false;
}
</script>
<script>
/*$(document).ready(function(){
$("#btnTest").click(function(){
$("#cardDrawerBox").animate({left: '200px'});
});
});*/
$("#cardDeckId").click(function(){
$('#cardDrawerBox').animate({
top: '300px',
}, 500, function() {
$('#cardDrawerBox').removeAttr('style');
});
});
</script>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将图像命名为像卡片一样,则可以使用路径中的卡名称获取文件:
public/cards/$ {cardid} .jpeg
或类似的东西If you name your images like your cards, you can simply get the file using the name of the card in the path:
public/cards/${cardID}.jpeg
or something like this