javascript - 未捕获的类型错误 : scrollIntoView is not a function

标签 javascript php html typeerror js-scrollintoview

我不明白为什么,但是 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()返回 DOM 元素的列表。现在,您正在尝试访问返回的 NodeList 上的方法 .scrollIntoView,该方法当然不存在。

您可能想使用 document.querySelector()它返回单个元素。

关于javascript - 未捕获的类型错误 : scrollIntoView is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54493864/

相关文章:

Php foreach数据插入

html - 在 HTML 元素中的单击位置获取文本

javascript - 使用css隐藏 slider 中的html元素

javascript - 使用@viewchild时无法读取 Angular 中未定义的属性 'nativeElement'

javascript - 单击 href 选择隐藏的单选按钮

javascript - 这个json格式怎么写呢?

没有 header 的 PHP Soap 基本授权

javascript - 根据可作为数组使用的动态键更新 json 对象中的值

php - 如何从 php 向 Titanium mobile 发送和接收多维数组?

javascript - 尝试查找以前的 DOM 元素并获取输入值 jquery