简体   繁体   English

如何在两个或多个 dom 元素上 append 图像?

[英]How to append an Image on two or more dom elements?

I'm finishing up my battleship project and want to add ship images.我正在完成我的战舰项目并想添加船舶图像。 Before if a player places a ship at a certain coordinate, I just change the color of the divs as an indication of ship being placed.之前,如果玩家将一艘船放置在某个坐标处,我只是更改 div 的颜色作为放置船的指示。 Now I want to place ( append ) images dynamically.现在我想动态放置( append )图像。

            <div class="gameBoard">
                <div class="grid"></div>
                <div class="grid"></div>
                <div class="grid"></div>
                <div class="grid"></div>
                <div class="grid"></div>
                <div class="grid"></div>
            </div>

The div gameboard above represents my board in the dom.上面的 div 游戏板代表我在 dom 中的棋盘。 And each divs represent each individual boxs where the players will attack each other.每个 div 代表每个单独的盒子,玩家将在其中互相攻击。 So how can I add one images on lets say to or more divs, so that the image span across those divs.那么我怎样才能在让我们说到或更多 div 上添加一个图像,以便图像跨越这些 div。

Something like this?是这样的吗?

function addImage(a,b){
   document.querySelectorAll(".grid")[a].style.background = "url(ship.jpg)"; 
   document.querySelectorAll(".grid")[b].style.background = "url(ship.jpg)";
}
<div class="grid" onclick='addImage(3,4)'></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM