简体   繁体   English

从项目网格中获取属性

[英]Get an attribute from a grid of items

I have a game board 8x8. 我有一个8x8游戏板。 below is the code to create the board... 以下是创建电路板的代码...

for (var i = 0 ; i < TOTAL_ROWS ; i++) {
tab_imgs[i] = [];
for (var j = 0 ; j < TOTAL_COLUMNS ; j++) {
  var num_img = Math.ceil(Math.random() * NUM_IMGS);

  if (i > 1) {
    while(tab_imgs[i-2][j] == num_img && tab_imgs[i-1][j] == num_img){
      num_img = Math.ceil(Math.random() * NUM_IMGS);
    }
  }
  if (j > 1) {
    while(tab_imgs[i][j-2] == num_img && tab_imgs[i][j-1] == num_img){
      num_img = Math.ceil(Math.random() * NUM_IMGS);

      if (i > 1) {
        while(tab_imgs[i-2][j] == num_img && tab_imgs[i-1][j] == num_img){
          num_img = Math.ceil(Math.random() * NUM_IMGS);
        }
      }

    }
  }

  tab_imgs[i][j] = num_img;
  render_table += '<div class="jewel jewel_' + num_img + '" data-row="' + i + '" data-col="' + j + '" data-jewel="' + num_img + '" style="top: ' + Number(i*TOTAL_IMGS) + 'px; left: ' + Number(j*TOTAL_IMGS) + 'px;"></div>';
}
}

The code above will produce a random board. 上面的代码将产生一个随机板。 My issue is when the user resumes an existing game. 我的问题是用户恢复现有游戏的时间。 I need to loop through the ALREADY produced html and grab the attribute "data-jewel" so instead of these lines: 我需要遍历ALREADY产生的html并获取属性“ data-jewel”,而不是这些行:

num_img = Math.ceil(Math.random() * NUM_IMGS);

I have: 我有:

num_img = attribute('data-jewel') 

for each grid; 对于每个网格;

您可以使用jQuery .data()方法检索数据属性的值:

num_img = +$('.selector').data( 'jewel' );

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

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