简体   繁体   English

如何使表中的所有单元格在JavaScript中不可点击?

[英]How to make all of cells in table unclickable in JavaScript?

I've made simple minesweeper game in JavaScript (for school project). 我已经用JavaScript(适用于学校项目)制作了简单的扫雷游戏。

Its 5x5 matrix based with 10 mines placed randomly in in matrix. 它的5x5矩阵基于随机放置在矩阵中的10个地雷。 I've used 5x5 table for graphical representation, and I've added in every cell onclick function that checks if corresponding matrix cell is mine.If it isn't it colors the cell in yellow and writes how many neighboring cells are mines. 我使用5x5表格进行图形表示,并在每个单元格的onclick函数中添加了检查对应的矩阵单元格是否是我的矩阵的功能,如果不是,它将用黄色将该单元格上色并写入多少个相邻单元格是我的。

Now i need to make it if the cell is a mine all of the other cells become unclickable. 现在,如果该单元格是我的单元格,则需要使所有其他单元格变得不可点击。

Any assistance will be appreciated. 任何帮助将不胜感激。

I would add a class to the table to mark it as active and have your click handler use that class, upon clicking a bomb, I would remove that class and add a class that indicates the game has ended. 我将一个类添加到表中以将其标记为活动,并让您的单击处理程序使用该类,在单击炸弹时,我将删除该类并添加一个指示游戏已结束的类。

Start with: 从...开始:

<table class="game-active">

After clicking a bomb: 单击炸弹后:

<table class="game-over">

via this: 通过这个:

$('table.game-active').removeClass('game-active').addClass('game-over');

You can add an attribute to the cell (), which will represent the boolean checkable property, and check that property on the click event function. 您可以向单元格()添加一个属性,该属性将表示布尔值checkable属性,然后在click事件函数上检查该属性。 In case you're using jQuery you can use the data() function to set and get that property. 如果您使用的是jQuery,则可以使用data()函数来设置和获取该属性。 You use it like that: Get: 您可以这样使用:获取:

if($(this).data("propName")==booleanValue){
   // write your code here
}

Set: $(this).data("propName",booleanValue"); 设置:$(this).data(“ propName”,booleanValue“);

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

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