[英]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.