所以我一直在研究一艘战舰游戏,但是我遇到了一个我一直无法解决的问题。当所有8艘船被发现时,我想提醒一个获胜的消息,在玩家赢得之前发射的总次数。然后我想删除进一步点击表示方块的tds的能力,从而模拟“游戏结束”。下面的代码只会阻止警报显示多次,但您仍然可以单击这些方块以执行其他代码。这我想摆脱。如何选择所有td元素,并使所有代码附加到单击事件停止工作?也许有更简单的方法来创造一个“游戏结束”而不是这个?任何帮助表示赞赏。如何在战列舰游戏结束时从所有<tds>中删除事件监听器?
编辑这里有一个小提琴:http://jsfiddle.net/pfb8dc83/1/
if (hitsCounter == 8) {
alert('Congratulations! You sank all battleships!');
alert('You fired ' + totalShots + ' rounds before you won the battle of the sea.');
var all = document.getElementsByTagName('table')[0].getElementsByTagName('td');
all.length.removeEventListener('click', play); //getting an error here saying it's not a function, because of the .length probably.
}
为什么不干脆把一个监听器在桌子上?搜索[*活动委托*](https://www.google.com/search?client=safari&rls=zh-CN&q=javascript+event+delegation&ie=UTF-8&oe=UTF-8&gfe_rd=cr&ei=ymYmVPjFJKuN8Qfe94HABg)。 – RobG 2014-09-27 07:27:33