-1
當我單擊每個單元格時,它會給出列,但不是行。我相信不知何故,我無法訪問應該是td的parentNode,以便我可以得到正確的行號。未捕獲的類型錯誤:無法讀取未定義的屬性'rowIndex'
這裏是我的javascript代碼:
function getBoard(rows, cols)
{
var board="";// ="<table>";// id=myTableId>"; //add this
var switch1 = 0;
for (x=0; x<rows; x++)
{
board +="<tr>";
for (y=0; y<cols;y++)
{
switch1 = x%2;
if (y%2 == switch1)
board +="<td class=\"" + "td1\">" ;
else
board +="<td class=\"" + "td2\">";
}
board +="</tr>";
}
return board;
}
function setEvents()
{
var cells= document.getElementsByTagName("td");
for (var x = 0; x<cells.length; x++)
{
cells[x].onclick=function() {
var col = this.cellIndex;
var row = this.ParentNode.rowIndex;
alert (row);
var info1=document.getElementById("coordinates");
var row = this.ParentNode.rowIndex;
info1.innerHTML="\"" + row +"\"";
};
}
}
window.onload = function()
{
var divGrid = document.getElementById("myTableId");
var board = getBoard(8,8);
//divGrid.innerHTML ="debug information";
divGrid.innerHTML = board;
setEvents();
}
,這裏是我的html:
<!DOCTYPE html>
<title> Chessboard </title>
<head>
<link rel="stylesheet" href="styles.css">
<script src="view.js"></script>
<script src="model.js"></script>
</head>
<body >
<table id="myTableId">
<div id ="gridDiv"> </div>
</table>
<div id = "coordinates"></div>
</body>
它是'.parentNode',小寫'p'。 – Pointy 2014-10-21 22:47:12