我正在制作一款名为2048比特币的游戏,我正在尝试禁用此代码:<a id="restart-button" class="restart-button">Cash Out</a>
,然后在<div class="satoshis-container">0</div>
等于1或更大时启用它。但我试过这样做,但没有奏效:
在index.html中:<a id="restart-button" class="restart-button" disabled>Cash Out</a>
在js/grid.js中(在移除瓦片的部分中(:
Grid.prototype.removeTile = function (tile) {
this.cells[tile.x][tile.y] = null;
if (document.querySelector("#satoshis-container").value > 1) {
document.querySelector("#restart-button").disabled = false;
}
};
当容器的值为0时,如何禁用按钮并重新启用它?
您不需要布尔值上的if,但是您的div没有值。试试这个,我使用一元加号将字符串内容强制转换为数字
Grid.prototype.removeTile = function (tile) {
this.cells[tile.x][tile.y] = null;
document.querySelector("#restart-button").disabled = +document.querySelector("#satoshis-container").textContent < 1;
};