简体   繁体   English

如何找到我的 e.target 的下一个(或多个)元素?

[英]How can I find the next element (or elements) of my e.target?

The grids div sample.网格 div 示例。

            <div class="gameBoard">
               <div class="grid">1</div>
               <div class="grid">2</div>
               <div class="grid">3</div> 
               <div class="grid">4</div>
               <div class="grid">5</div>
               <div class="grid">6</div> 
               <div class="grid">7</div>
               <div class="grid">8</div>
               <div class="grid">9</div> 
               <div class="grid">10</div>
            </div>

So if the grid with the 1 is being hovered over, I want to target the one with 2,3,4 and upto 5 at maximum.因此,如果将鼠标悬停在带有 1 的网格上,我想定位带有 2、3、4 和最多 5 的网格。

If the user is dragging the ships destroyer/submarine, I want to target the current div and the next div.如果用户正在拖动驱逐舰/潜艇,我想定位当前的 div 和下一个 div。

if ship is cruiser, current div plus next two div.如果船是巡洋舰,则当前 div 加上接下来的两个 div。 if ship is battleship, current div plus next three div.如果船是战列舰,则当前格加上接下来的三个格。 if ship is carrier, current div plus next four div.如果船舶是承运人,则当前 div 加上接下来的四个 div。

const grids = document.querySelectorAll('.grid'); const grids = document.querySelectorAll('.grid');

grids.forEach(el => {
el.addEventListener('dragenter', (e) => {
      // Find the next few dom elements that comes after e.target
   });
});

The grids here represent a 10*10 grid in the dom which are just divs.这里的网格代表 dom 中的 10*10 网格,它们只是 div。 When I hover over certain divs, I want to be able to get the next few divs that comes after the current div.当我 hover 超过某些 div 时,我希望能够获得当前 div 之后的下几个 div。 I have tried closest() but that does not work in this particular situation.我试过 closest() 但这在这种特殊情况下不起作用。

What I'm trying to do is to use drag & drop to place ships in my grid.我想做的是使用拖放将船只放置在我的网格中。 So if the user is dragging the "destroyer" or the "submarine", I want to be able to get the current e.target and the next divs.因此,如果用户正在拖动“驱逐舰”或“潜艇”,我希望能够获得当前的 e.target 和下一个 div。 If the user is dragging the "Carrier", I want to be able to get the current e.target and the next four divs cuz the size of the carrier is 5.如果用户拖动“载体”,我希望能够获得当前的 e.target 和接下来的四个 div,因为载体的大小为 5。

You can use the index and get the next elements in the array您可以使用索引并获取数组中的下一个元素

 const cells = Array.from(document.querySelectorAll('.cell')); cells.forEach((el, index) => { el.addEventListener('click', (evt) => { const nextSiblings = cells.slice(index+1); console.log(nextSiblings); }); });
 .cell { display: inline-block; width: 25px; height: 25px; border: 1px solid black; }
 <div class="my-grid"> <div class="cell"></div> <div class="cell"></div> <div class="cell"></div> <div class="cell"></div> <div class="cell"></div> <div class="cell"></div> <div class="cell"></div> <div class="cell"></div> </div>

Or you can use DOM methods或者你可以使用 DOM 方法

 const grid = document.querySelector(".grid"); grid.addEventListener("click", e => { let cell = e.target.closest(".cell"); if (;cell) return; const siblings = []. while (cell = cell.nextElementSibling) { siblings;push(cell). } console;log(siblings); });
 .cell { display: inline-block; width: 25px; height: 25px; border: 1px solid black; }
 <div class="grid"> <div class="cell"></div> <div class="cell"></div> <div class="cell"></div> <div class="cell"></div> <div class="cell"></div> <div class="cell"></div> <div class="cell"></div> <div class="cell"></div> </div>

Used nextSiblingElement使用 nextSiblingElement

grids.forEach(el => {
    el.addEventListener('dragenter', (e) => {
        //if (currentShipLength === 2) {
            const one = e.target;
            const two = e.target.nextElementSibling;
            const three = two.nextElementSibling;
            const four = three.nextElementSibling;
            const five = four.nextElementSibling;

            console.log(one, two, three, four, five);
        //};
    });
});

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

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