[英]Get an attribute from a grid of items
我有一個8x8游戲板。 以下是創建電路板的代碼...
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>';
}
}
上面的代碼將產生一個隨機板。 我的問題是用戶恢復現有游戲的時間。 我需要遍歷ALREADY產生的html並獲取屬性“ data-jewel”,而不是這些行:
num_img = Math.ceil(Math.random() * NUM_IMGS);
我有:
num_img = attribute('data-jewel')
對於每個網格;
您可以使用jQuery .data()
方法檢索數據屬性的值:
num_img = +$('.selector').data( 'jewel' );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.