[英]How do I access DOM nodes from an array in javascript?
我正在嘗試構建一個下拉菜單,當您將鼠標懸停在其中一個單元格上時,該菜單的側面將顯示一行。 我對使用DOM和javascript真的很陌生。
我遇到的問題是,我創建的數組似乎無法訪問我認為應該訪問的內容。 僅僅是我的語法,還是我需要以不同的方式處理? 這是我的代碼:
window.onload = function () {
var tableID = "strokerKitMenu";
var table = document.getElementById(tableID);
var tableRows = table.getElementsByTagName("tr");
var tableCell = [];
var counter = 0;
for (i = 0; i < tableRows.length; i++) {
tableCell[i] = new Array();
tableCell[i].push(tableRows[i].getElementsByTagName("td"));
}
for (i = 0; i < tableCell.length; i++) {
tableCell[0, i].style.display = "block"; //This doesn't compile
alert(tableCell[0, i].Text); //This comes back "undefined"
for (j = 1; j < tableCell[i].length; j++) //I haven't even tested this part yet
{
tableCell[i][j].onmouseover = function showCell() {
tableCell[i][j + 1].style.display = "block";
}
tableCell[i][j].onmouseout = function hideCell() {
this.style.display = "none";
}
} //end inner for loop
} //end outer for loop
}
tableCell[0, i].style.display="block"; //This doesn't compile
該語法在JavaScript中無效:您為tableCell
指定了兩個索引,但我想您的意思是tableCell[0][i].style.display="block";
alert(tableCell[0, i].Text); //This comes back "undefined"
修復第一個錯誤后,您將需要將其更改為tableCell[0][i].textContent
。
tableCell[i].push.apply(tableCell[i], tableRows[i].getElementsByTagName("td"));
您不想推入單個數組。您想推入所有元素。否則,您將不得不作為tableCell[i][0][j]
訪問它們
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.