简体   繁体   English

如何从JavaScript中的数组访问DOM节点?

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

相关问题 如何将DOM节点递归到Javascript中的任意深度? - How do I recurse DOM nodes to an arbitrary depth in Javascript? 如何使用javascript中的鼠标悬停事件删除我在javascript中创建的DOM节点? - How do I remove DOM nodes that I created in javascript using mouseover events in javascript? 当已选择数组中的DOM元素时,如何阻止javascript函数运行? - How do I prevent a javascript function from running when a DOM element from an array is already selected? 在JavaScript中,如何使用[]运算符访问从Array继承的对象? - In JavaScript, how do I access an object inherited from Array with the [] operator? 如何从JavaScript访问COM字节数组 - How do I access a COM byte array from JavaScript 在JQuery中,如何访问DOM元素数组? - In JQuery, how do I access an array of DOM elements? 如何从Javascript数组中获取两个最大的整数并将值返回到DOM? - How do I grab the two largest integers from a Javascript Array and return the value to the DOM? 如何将javascript创建的节点输入数组 - how do I input javascript created nodes into an array 如何在javascript中访问此关联数组? - How do I access this associative array in javascript? 如何在JavaScript中访问数组中的数据 - How do I access data in an array in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM