简体   繁体   English

如何使用JavaScript函数为每个div分配坐标值?

[英]How can I assign each div a coordinate value with a javascript function?

I've a function that generates div s and appends them to a 'container' div, and it assigns each div with a unique ID. 我有一个生成div并将其附加到“容器” div的函数,它为每个div分配了唯一的ID。 I use a for loop to run the function and fill the 'container' div , which is just wide enough so that the tiles being added create a square. 我使用for循环运行该函数并填充'container'div,该div足够宽,因此要添加的图块会创建一个正方形。

The following is my code to generate a div : 以下是我生成div代码:

function cdiv(ele) {
  var div = document.createElement("div");
  div.className = 'tiles';
  div.id = 'div' + id++; // det unique id and increment id value
  div.addEventListener("click", clr);
  div.dataset.taps = x;
  div.dataset.xcoordinate = 0;
  div.dataset.ycoordinate = 0;
  ele.appendChild(div);
}

I use the following to contain the div s being created: 我使用以下内容包含正在创建的div

var div = document.getElementById('container');

for (i = 0; i < 100; i++) {
  for (b = 0; b < 100; b++) { 
    cdiv(div);
  }
}

Note - my 'container' div is 5000px wide so that if each div being created is 50px wide, I will then have a square grid of div s. 注意-我的“容器” div的宽度为5000px,因此,如果要创建的每个div的宽度为50px,则将有一个div的正方形网格。

My problem is being able to assign each div a coordinate. 我的问题是能够为每个div分配一个坐标。 I need a function that I can use to pass through to the x/y coordinate tag s in each div being created, and give them a value depending on where the div is located in the 'grid'. 我需要一个函数,该函数可用于传递到正在创建的每个div中的x / y坐标tag s,并根据div在“网格”中的位置为其提供一个值。 The number of tiles is variable so please don't give a solution for just a 100 by 100 grid. 瓦片的数量是可变的,所以请不要只为100 x 100网格提供解决方案。

Not sure if I understand the question fully. 不知道我是否完全理解这个问题。

Would this work? 这行得通吗?

function cdiv(ele, xcoord, ycoord) {
  var div = document.createElement("div");
  div.className = 'tiles';
  div.id = 'div' + id++; // det unique id and increment id value
  div.addEventListener("click", clr);
  div.dataset.taps = x;
  div.dataset.xcoordinate = xcoord;
  div.dataset.ycoordinate = ycoord;
  ele.appendChild(div);
}

Then 然后

var div = document.getElementById('container');

for (i = 0; i < 100; i++) {
  for (b = 0; b < 100; b++) { 
    cdiv(div, i, b);
  }
}

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

相关问题 如何在javascript或jQuery中为每个div分配JS数组值? - How to assign JS array value to each div in javascript or Jquery? 如何为JavaScript变量分配坐标,并使用moveTo和lineTo在这些变量周围绘制线? - How can I assign a coordinate to a javascript variable, and draw lines using moveTo and lineTo around these variables? 如何在JavaScript中为数组中的每个对象分配一个随机数? - How can I assign a random number to each object in an array in javascript? 每次我将变量分配给 javascript 中的不同值时,如何将变量传递给 function - How can I pass a variable to a function every time I assign it to a different value in javascript 如何将每个功能或方法分配给按钮? - How can i assign each function or method to a button? 如何为 javascript 中的 div 分配月日值? - how to assign month days value to div in javascript? 如何为会话值分配JavaScript变量 - How can I assign a javascript variable to the session value 如何通过 id 将获取元素的值分配到 Javascript 中的数组中? - How can I assign value of get element by id into array in Javascript? 如何使用JavaScript将功能分配给元素? - How can I assign a function to an element using JavaScript? 在webpy中,如何将javascript值分配给python变量? - In webpy, how can I assign a javascript value to a python variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM