Uncaught TypeError: scrollIntoView is not a function

Uncaught TypeError: scrollIntoView is not a function

我不知道为什么,但是 scrollIntoView() 功能不起作用。浏览器显示错误:Uncaught TypeError: element.scrollIntoView is not a function at ...。元素 element 已定义并显示在控制台中。我无法在互联网上找到任何答案。下面是简化的代码:

[...]
<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Surname</th>
        </tr>
    </thead>
    <tbody>
        <?php 
        if (is_array($array)) {
            foreach ($array as $element) { ?>
                <tr data-someid="<?= $element->some_id ?>">
                    <th><?= $element->id ?></th>
                    <td><?= $element->name ?></td>
                    <td><?= $element->surname ?></td>
                </tr>
            <?php } 
        } ?>
    </tbody>
</table>
[...]
<script>
    var element= document.querySelectorAll('[data-someid="' + someId + '"]');
    console.log(element); // displays the element well
    element.scrollIntoView();
</script>
[...]

有人知道为什么它不起作用吗?

document.querySelectorAll() returns list DOM 个元素。现在您正在尝试访问返回的 NodeList 上的方法 .scrollIntoView,这当然不存在。

您可能打算使用 document.querySelector() 其中 returns 一个元素。