如何在jQuery语法下使用scrollIntoView()?

时间:2017-06-27 20:42:27

标签: javascript jquery html

我需要让最后一个回复使用jQuery滚动到视图中。这是我的代码:

function showHideReplies(replies){
 replies.each(function() {
     if($(this).index() < nIniRep || afterReply){
            $(this).show();
            if(afterReply && $(this).index()==replies.length ){
                $(this).scrollIntoView();
            }
        }else{
            $(this).hide();
        }

 });

}

afterReply已在我的JavaScript中定义。

我的问题是:我知道scrollIntoView()是一个HTML DOM方法,您可以像这样使用它:

var elmnt = document.getElementById("content");
elmnt.scrollIntoView();

但我需要使用jQuery,我认为它应该以相同的方式工作。但它不适用于jQuery语法$(this).scrollIntoView();

如何在jQuery语法下使用scrollIntoView()

谢谢。

1 个答案:

答案 0 :(得分:4)

jQuery&#39; $(this)返回jQuery object。要定位此元素本身,您需要使用[0]访问对象的第一个索引。从本质上讲,jQuery的$(this)[0]相当于JavaScript this

因此,您正在寻找:

$(this)[0].scrollIntoView();

希望这有帮助! :)