简体   繁体   English

通过 JavaScript 获取特定的数据属性?

[英]Get specific data- attribute through JavaScript?

I am trying to find the data-x and data-y values of the adjacent tiles to a tile from a map that I have created, and I have only been able to return the data values from the targeted tile.我试图从我创建的地图中找到与图块相邻图块的 data-x 和 data-y 值,但我只能从目标图块返回数据值。 How am I able to reference the specific div element that, for example, has the value of data-x clicked - 1, and then return the element that is at that location.我如何能够引用特定的 div 元素,例如,点击 data-x 的值 - 1,然后返回位于该位置的元素。

The HTML code for defining the custom data attributes are as follows:定义自定义数据属性的 HTML 代码如下:

 <div class="grass gameTile" data-x="0" data-y="0"></div>
            <div class="grass gameTile" data-x="1" data-y="0"></div>
            <div class="grass gameTile" data-x="2" data-y="0"></div>
            <div class="grass gameTile" data-x="3" data-y="0"></div>

...and so on. ...等等。

This image explains what I mean more clearly, as I am trying to click on the red div and find the values of data-x and data-y of the green divs to the top, bottom, left and right of the red div.这张图片更清楚地解释了我的意思,因为我试图点击红色 div 并找到红色 div 顶部、底部、左侧和右侧的绿色 div 的 data-x 和 data-y 的值。

Instead of adding clicks to every element, you can add one to the parent.您可以向父元素添加一个,而不是向每个元素添加点击。 US ent target to get what was clicked. US ent target 以获取点击的内容。 Than you can read the attributes.比你可以读取属性。 Than it is a simple querySelector with attributes to get the elements.它是一个简单的 querySelector,具有获取元素的属性。

 document.getElementById("board").addEventListener("click", function (evt) { var tile = evt.target, x = +tile.getAttribute("data-x"), y = +tile.getAttribute("data-y"), tileTop = document.querySelector('[data-x="'+ (x) +'"][data-y="' + (y-1) + '"]'), tileBot = document.querySelector('[data-x="'+ (x) +'"][data-y="' + (y+1) + '"]'), tileRight = document.querySelector('[data-x="'+ (x+1) +'"][data-y="' + (y) + '"]'), tileLeft = document.querySelector('[data-x="'+ (x-1) +'"][data-y="' + (y) + '"]'); console.log(tileTop, tileBot, tileRight, tileLeft); });
 .gameTile { background-color: green; border: 1px solid black; display: inline-block; width: 20px; height: 20px; }
 <div id="board"> <div class="grass gameTile" data-x="0" data-y="0"></div> <div class="grass gameTile" data-x="1" data-y="0"></div> <div class="grass gameTile" data-x="2" data-y="0"></div> <div class="grass gameTile" data-x="3" data-y="0"></div> <br/> <div class="grass gameTile" data-x="0" data-y="1"></div> <div class="grass gameTile" data-x="1" data-y="1"></div> <div class="grass gameTile" data-x="2" data-y="1"></div> <div class="grass gameTile" data-x="3" data-y="1"></div> <br/> <div class="grass gameTile" data-x="0" data-y="2"></div> <div class="grass gameTile" data-x="1" data-y="2"></div> <div class="grass gameTile" data-x="2" data-y="2"></div> <div class="grass gameTile" data-x="3" data-y="2"></div> <br/> <div class="grass gameTile" data-x="0" data-y="3"></div> <div class="grass gameTile" data-x="1" data-y="3"></div> <div class="grass gameTile" data-x="2" data-y="3"></div> <div class="grass gameTile" data-x="3" data-y="3"></div> </div>

Here is a alternate that uses jQuery.这是一个使用 jQuery 的替代方法。

 $('.gameTile').click(function() { console.log($(this).attr('data-x')); console.log($(this).attr('data-y')); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="grass gameTile" data-x="0" data-y="0">0</div> <div class="grass gameTile" data-x="1" data-y="0">1</div> <div class="grass gameTile" data-x="2" data-y="0">2</div> <div class="grass gameTile" data-x="3" data-y="0">3</div>

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

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