[英]How do I access DOM nodes from an array in javascript?
I am trying to build a drop-down menu that will show a row to the side of it when you hover over one of the cells with the mouse. 我正在尝试构建一个下拉菜单,当您将鼠标悬停在其中一个单元格上时,该菜单的侧面将显示一行。 I'm really new with using the DOM, and javascript as well.
我对使用DOM和javascript真的很陌生。
The problem I'm running into is that the array I have created doesn't seem to access what I thought it should. 我遇到的问题是,我创建的数组似乎无法访问我认为应该访问的内容。 Is it just my syntax, or do I need to approach this differently?
仅仅是我的语法,还是我需要以不同的方式处理? Here is my code:
这是我的代码:
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
This syntax is invalid in JavaScript: you have specified two indices for tableCell
but I guess you meant tableCell[0][i].style.display="block";
该语法在JavaScript中无效:您为
tableCell
指定了两个索引,但我想您的意思是tableCell[0][i].style.display="block";
alert(tableCell[0, i].Text); //This comes back "undefined"
Once you fix the first error, you will want to change this to tableCell[0][i].textContent
. 修复第一个错误后,您将需要将其更改为
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.