可以通过 event.target 添加图片吗?

huangapple go评论54阅读模式
英文:

Is it possible to add an image through event.target?

问题

以下是代码的翻译部分:

我想在一个7x7的网格中制作一个简单的游戏,我希望在单击时从一个数组中放置图像,然后检查匹配(就像连接四一样)。

我有一个网格,其中所有的方块都是HTML divs,如下所示:

<div class="grid">
      <div class="square" id="1"></div>
      <div class="square" id="2"></div>
      <!-- 其他方块 -->
      <div class="square" id="49"></div>
</div>

还有一些存储在app.js中的图像数组:

document.addEventListener("DOMContentLoaded", () => {
    const squares = document.querySelectorAll(".square");
    const scoreDisplay = document.getElementById("result");
    let score = 0;
    squares[24].style.backgroundImage = "url('res/middle.png')";
    const cards = [
        {
            name: 'blue',
            img: 'res/blue.png'
        }, 
         {
            name: 'yellow',
            img: 'res/yellow.png'
        }, 
        {
            name: 'red',
            img: 'res/red.png'
        }, 
        {
            name: 'orange',
            img: 'res/orange.png'
        }, 
        {
            name: 'green',
            img: 'res/green.png'
        }, 
        {
            name: 'pink',
            img: 'res/pink.png'
        }, 
        {
            name: 'ivory',
            img: 'res/ivory.png'
        }, 
    ];

    // 随机图像不会显示在网格中,但分数会更新,没有控制台错误。
    let randomCard = Math.floor(Math.random() * cards.length);

    squares.forEach(square => {
        square.addEventListener('click', function(e) {
            if (square.id !== squares[24].id) {
                score++;
                scoreDisplay.textContent = score;
                e.target.style.backgroundImage = `url('${randomCard.img}')`;
            } else {
                // 这里可以添加处理特殊情况的代码
            }
        });
    });
});

希望这对你有所帮助。如果你需要进一步的帮助或有其他问题,请随时告诉我。

英文:

I want to make a simple game in a 7x7 grid, where I want to place on click images from an array, and then check for matches (like in connect four)

I have a grid, with all its squares in HTML divs, like this :

&lt;div class=&quot;grid&quot;&gt;
      &lt;div class=&quot;square&quot; id=&quot;1&quot;&gt;&lt;/div&gt;
      &lt;div class=&quot;square&quot; id=&quot;2&quot;&gt;&lt;/div&gt;
      &lt;...............................&gt;
      &lt;div class=&quot;square&quot; id=&quot;49&quot;&gt;&lt;/div&gt;

and some images stored in an array in app.js :

document.addEventListener(&quot;DOMContentLoaded&quot;, () =&gt; {
    const squares = document.querySelectorAll(&quot;.square&quot;)
    const scoreDisplay = document.getElementById(&quot;result&quot;)
    let score = 0;
    squares[24].style.backgroundImage = &quot;url(&#39;res/middle.png&#39;)&quot;
    const cards = [
        {
            name: &#39;blue&#39;,
            img: &#39;res/blue.png&#39;
        }, 
         {
            name: &#39;yellow&#39;,
            img: &#39;res/yellow.png&#39;
        }, 
        {
            name: &#39;red&#39;,
            img: &#39;res/red.png&#39;
        }, 
        {
            name: &#39;orange&#39;,
            img: &#39;res/orange.png&#39;
        }, 
        {
            name: &#39;green&#39;,
            img: &#39;res/green.png&#39;
        }, 
        {
            name: &#39;pink&#39;,
            img: &#39;res/pink.png&#39;
        }, 
        {
            name: &#39;ivory&#39;,
            img: &#39;res/ivory.png&#39;
        }, 
    ]

The random image of the array does not appear on the grid, however the score updates and there are no console errors.

let randomCard = Math.floor(Math.random() * cards.length)
   

//    clickedPosition = ?

   squares.forEach(square =&gt; {
        square.addEventListener(&#39;click&#39;, function(e) {
            if(square.id !== squares[24] ) {
                score ++;
                scoreDisplay.textContent = score
                e.target.style.backgroundImage = randomCard.img
                

            } else {

            }
        })
   })

})

答案1

得分: 0

I figured it out!

将 cards 数组更改为如下所示:

const cards = [
"url('res/blue.png')",
"url('res/yellow.png')",
"url('res/red.png')",
"url('res/orange.png')",
"url('res/green.png')",
"url('res/pink.png')",
"url('res/ivory.png')"
]

现在,单个图像将在点击时显示(不幸的是不是随机的,但这是进展!)

e.target.style.backgroundImage = cards[randomCard]

英文:

I figured it out!

Changed the cards array to look like this :

> const cards = [
> "url('res/blue.png')",
> "url('res/yellow.png')",
> "url('res/red.png')",
> "url('res/orange.png')",
> "url('res/green.png')",
> "url('res/pink.png')",
> "url('res/ivory.png')"
> ]

and now a single image will appear on click (unfortunately not a random one, but this is progress! )

 e.target.style.backgroundImage = cards[randomCard]

huangapple
  • 本文由 发表于 2023年4月4日 18:08:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75928104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定