【问题标题】:"querySelectorAll" on HTML table not working as expectedHTML 表上的“querySelectorAll”未按预期工作
【发布时间】:2021-06-21 18:08:51
【问题描述】:

我的任务是在 HTML 表 tr 元素上使用 querySelectorAll,使用 nth-child 为偶数和奇数打印不同的颜色,但只有偶数有效。下面是我的代码

我也收到“Uncaught TypeError: Cannot read property 'style' of undefined”的错误,

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <table>
    <thead>
        <tr>
        <th>Numbers</th>
        </tr>
    </thead>
    <tbody>
        <tr>
        <td>1 = odd</td>
        </tr>
        <tr>
        <td>2 = even</td>
        </tr>
        <tr>
        <td>3 = odd</td>
        </tr>
        <tr>
        <td>4 = even</td>
        </tr>
    </tbody>
    </table>
    <button onclick='execute()'>Execute</button>
</body>
<script>
    function execute() {
        // your code goes here
    var odd = document.querySelectorAll("tbody tr:nth-child(odd)")
    var even = document.querySelectorAll("tbody tr:nth-child(even)")
for(i = 0; i<= odd.length; i++){
  odd[i].style.backgroundColor = "red"
}
for(i = 0; i<= even.length; i++){
  even[i].style.backgroundColor = "green"
}
    }

</script>
</html>

【问题讨论】:

    标签: javascript html css node.js reactjs


    【解决方案1】:

    循环条件中有一个错误:通过使用&lt;=,您告诉JS 从0 迭代到等于odd.length 的值。但是,odd[odd.length] 没有任何内容,因为它的索引范围从 0 到 odd.length - 1,因为数组是 0 索引的。这就是为什么您会收到您所看到的错误 - 不存在的数组元素始终评估为 undefined,其属性无法引用。

    由于第一个循环抛出错误,JavaScript 结束执行并且永远不会进入第二个循环。这就是“偶数”行不起作用的原因。

    【讨论】:

    • 谢谢。我无法投票,因为我的声望少于 15 个
    【解决方案2】:

    您想要i &lt; odd.length 而不是i &lt;= odd.length(对偶数进行同样的更改)。 length 将是最后一个索引加一,因为数组索引是从零开始的。

    超出数组的末尾会导致错误,这会停止执行,因此您看不到任何原本会发生的更改。

    仅适用于这些更改。

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <body>
        <table>
        <thead>
            <tr>
            <th>Numbers</th>
            </tr>
        </thead>
        <tbody>
            <tr>
            <td>1 = odd</td>
            </tr>
            <tr>
            <td>2 = even</td>
            </tr>
            <tr>
            <td>3 = odd</td>
            </tr>
            <tr>
            <td>4 = even</td>
            </tr>
        </tbody>
        </table>
        <button onclick='execute()'>Execute</button>
    </body>
    <script>
        function execute() {
            // your code goes here
        var odd = document.querySelectorAll("tbody tr:nth-child(odd)")
        var even = document.querySelectorAll("tbody tr:nth-child(even)")
    for(i = 0; i< odd.length; i++){
      odd[i].style.backgroundColor = "red"
    }
    for(i = 0; i< even.length; i++){
      even[i].style.backgroundColor = "green"
    }
        }
    
    </script>
    </html>

    【讨论】:

    • 谢谢。我不能投票,因为我的声望少于 15 个
    • 如果它解决了您的问题,您仍然可以将其标记为已接受的答案。
    • 我才知道这个
    猜你喜欢
    • 2014-11-14
    • 2023-03-21
    • 2017-02-13
    • 2018-08-15
    • 1970-01-01
    • 2020-10-23
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多