gpt4 book ai didi

javascript - 使用扫雷游戏的右键功能放置旗帜

转载 作者:行者123 更新时间:2023-12-03 11:16:47 25 4
gpt4 key购买 nike

仍在寻求更多帮助

好吧,我已经完成了使用 java 脚本构建的扫雷程序,所以现在要做的最后一件事就是在炸弹方 block 上放置旗帜。通过使用右键单击功能,我收到以下错误:

错误:“document.getElementById(...)”为 null 或不是对象

这是获取它的代码部分。现在我的图像位于一个文件夹中,标志图像名为 sqt01。因此,我多次更改了 document.getElementbyId 部分,将其指向不同的图像,但只是收到该错误。

//Global
//store the value of each square
var gaValue = new Array(8);
for (i = 0; i <= 8; i++) {
gaValue[i] = new Array(8);
}
for (i = 0; i <= 8; i++) {
//loop through each item in those row
for (j = 0; j <= 8; j++) {
gaValue[i][j] = 0;
}
}
//Store the status of each square
var gaSquare = new Array(8);
for (j = 0; j <= 8; j++) {
gaSquare[j] = new Array(8);
}
for (j = 0; j <= 8; j++) {
//loop through each item in those row
for (i = 0; i <= 8; i++) {
gaSquare[i][j] = 'c';
}
}
//Track of whether the game is over or not (starts this with false)
var gbGameOver = false;

function vInit()
{
var strHTML;
var i;
var j;

strHTML = "<table style='margin-left:auto;margin-right:auto'>";
strHTML += "<tr><td colspan='8' style='text-align:center'>MineSweeper</td></tr>";
strHTML += "<tr><td colspan='8' style='text-align:center'><input type='button' id='btnNew' value='New Game' onclick='vNewGame()'></td></tr>";

//Loop through the rows to build the table of tiles
for (i = 0; i < 8; i++)
{
strHTML += "<tr>";
for (j = 0; j < 8; j++)

strHTML += '<td><img src="images/sqt0.png" id="square' + i + ', ' + j + '" onclick="vClick(' + i + ', ' + j + ')" onmousedown="vRightClick(' + i + ', ' + j + ')"/></td>';

strHTML += "<tr>";
}
strHTML += '<tr><td colspan="8" style="text-align:center"><textarea id="taOut" cols="18" rows="10"></textarea></td></tr>';
strHTML += "</table>";
frmGrid.innerHTML = strHTML;

//Place bombs
var iBomb = 0;
var iRow = Math.floor(Math.random() * 8);
var iCol = Math.floor(Math.random() * 8);

while (iBomb < 8)
{
while (gaValue[iRow][iCol] == 9)
{
iRow = Math.floor(Math.random() * 8);
iCol = Math.floor(Math.random() * 8);
}
gaValue[iRow][iCol] = 9;
iBomb++;
}
//Calculate clue values around mines
var iMine = 0;
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
for (k = (i - 1) ; k <= (i + 1) ; k++)
for (m = (j - 1) ; m <= (j + 1) ; m++)
if (k >= 0 && k <= 7 && j >= 0 && j <= 7)
if (gaValue[k][m] == 9)
iMine++;
if (gaValue[i][j] != 9)
gaValue[i][j] = iMine;
iMine = 0;
}
}
}
//Get the ID of the image I need to change
function vClick(iRow, iCol)
{
var strID = "square" + iRow + ", " + iCol;
var strOut = "Gameover";

if (gbGameOver == false)
{

gaSquare[iRow][iCol] = 'o';
document.getElementById(strID).src = "images/" + gaValue[iRow][iCol] + ".png";

if (gaValue[iRow][iCol] == 9)
{
document.getElementById('taOut').value = strOut;
vOver();
}
if (gaValue[iRow][iCol] == 0)
{
vZero2(iRow, iCol);
}
}
}

//GameOver
function vOver()
{
var i;
var j;
var strID;

for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++)
{
strID = "square" + i + ", " + j;
document.getElementById(strID).src = "images/" + gaValue[i][j] + ".png";
}
}
//Clearing area
function vZero2(iRow, iCol)
{
var i;
var j;
for (i = iRow - 1; i <= (iRow + 1) ; i++)
{
for (j = iCol - 1; j <= iCol + 1; j++)
{
if (i >= 0 && i < 8 && j >= 0 && j < 8)
if (gaSquare[i][j] != 'o')
vClick(i, j);
}
}
}

//Start new game
function vNewGame()
{
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++)
{
gaValue[i][j] = 0;
gaSquare[i][j] = 'c';
document.getElementById('square' + i + ', ' + j).src = 'images/sqt0.png';
}
gbGameOver = false;
vInit();
}
//no menu on right click
function bIsRightButtonClicked(e) {
var rightclick = false;
e = e || window.event;
if (e.which) {
rightclick = (e.which == 3);
} else if (e.button) {
rightclick = (e.button == 2);
}
return rightclick;
}
//Getting Flag when you right click
function vRightClick(iRow, iCol, e)
{
if (bIsRightButtonClicked(e))
{
gaSquare[iRow][iCol] = 'c';
gaSquare[iRow][iCol] = 'f';
document.getElementById('square' + i + ', ' + j).src = 'images/sqt1.png'; // Error: 'document.getElementById(...)' is null or not an object
vOver();
}
}


//Getting Flag when you right click
function vRightClick(iRow, iCol, e)
{
if (bIsRightButtonClicked(e))
{
gaSquare[iRow][iCol] = 'c';
gaSquare[iRow][iCol] = 'f';
document.getElementById('square' + i + ', ' + j).src = 'images/sqt1.png'; // Error: 'document.getElementById(...)' is null or not an object
vOver();
}
}

最佳答案

dom 元素 ID 属性中不能有空格或逗号(请参阅 this post for more details and references )。它必须以以下内容开头:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".")

这样,我会将您 ID 中的“,”替换为“_”

您的代码需要更改为的一些示例:

...

strHTML += '<td><img src="images/sqt0.png" id="square' + i + '_' + j +
'" onclick="vClick(' + i + ', ' + j +
')" onmousedown="vRightClick(' + i + ', ' + j + ')"/></td>';

...

document.getElementById('square' + i + '_' + j)

...

var strID = "square" + iRow + "_" + iCol;

...

请注意,onclickonmousedown 处理程序/代码生成保持不变

关于javascript - 使用扫雷游戏的右键功能放置旗帜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27307915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com