关联图像与数组元素结果

发布于 2025-01-30 03:48:38 字数 1374 浏览 3 评论 0原文

我正在使用简单的纸牌游戏来锻炼自己的技能。我设法通过阵列绘制了随机化和循环的卡。我的问题是。现在,我如何将每张卡与图像相关联,以将其渲染到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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉态萌生 2025-02-06 03:48:38

如果您将图像命名为像卡片一样,则可以使用路径中的卡名称获取文件: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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文