javascript scrollIntoView 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12722941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:58:34  来源:igfitidea点击:

scrollIntoView not defined

javascriptjquery.net

提问by user1177860

I have the following problem.

我有以下问题。

I have a tree view control in .NET and I have created a autocomplete search box to search the tree for a name - which then I can select and it highlights the item with a selected class.

我在 .NET 中有一个树视图控件,我创建了一个自动完成搜索框来搜索树的名称 - 然后我可以选择它并突出显示具有选定类的项目。

The tree view is pretty long so I have given it a height and overflow scroll.

树视图很长,所以我给了它一个高度和溢出滚动。

The problem is that I want view or scroll down to the selected item when I've searched for it.

问题是我想在搜索时查看或向下滚动到所选项目。

So i have created the following script to scrollIntoView it but this doesn't seem to work:

所以我创建了以下脚本来 scrollIntoView 它,但这似乎不起作用:

function search_itemSelected(sender, e) {
        var hdSearchID = $get('<%= hdSearchID.ClientID %>');
        hdSearchID.value = e.get_value();
        var selectedElement = $("div.node.cen.selected"); // This works
        if (selectedElement != null) {
            selectedElement[0].scrollIntoView = 10; // This keeps coming back as undefined
        }
    }

回答by Gabriele Petrioli

First:change

第一:改变

if (selectedElement != null)

to

if (selectedElement.length)

because $("div.node.cen.selected");will never return null. jQuery always returns a jQuery object (empty or not, but a jQuery object)

因为$("div.node.cen.selected");永远不会回来null。jQuery 总是返回一个 jQuery 对象(是否为空,但是一个 jQuery 对象

So in the case where it is empty, the selectedElement[0]will return undefined and thus the scrollIntoViewdoes not exist..

所以在它为空的情况下, theselectedElement[0]将返回 undefined ,因此scrollIntoView不存在..

Second:scrollIntoViewis a function and so you do not assign a value to it. You need to call it with

第二:scrollIntoView是一个函数,所以你不给它赋值。你需要用

selectedElement[0].scrollIntoView();

回答by Aelios

scrollIntoViewis used with element you want to show, like that :

scrollIntoView与要显示的元素一起使用,如下所示:

el.scrollIntoView(true);

Note:

笔记:

Scrolls the window until the element is visible.

滚动窗口直到元素可见。

Syntax:

句法:

document.all.elementID.scrollIntoView(param1)

document.all.elementID.scrollIntoView(param1)

Parameters: param1 Optional; true or false, indicating whether the top of the element is scrolled to the top of the window or the bottom of the element is scrolled to the bottom of the window.

参数: param1 可选;true 或 false,表示是元素顶部滚动到窗口顶部还是元素底部滚动到窗口底部。

Moreover, scrollIntoView()can only be used with some tags

此外,scrollIntoView()只能与某些标签一起使用

http://www.java2s.com/Code/JavaScriptReference/Javascript-Methods/scrollIntoViewisappliedto.htm

http://www.java2s.com/Code/JavaScriptReference/Javascript-Methods/scrollIntoViewisappliedto.htm