我需要使用 jQuery 让最后的回复滚动到 View 中。这是我的代码:
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()
?
谢谢。
最佳答案
jQuery 的 $(this)
返回 jQuery object .要定位此元素本身,您需要使用 [0]
访问对象的第一个索引。本质上,jQuery 的 $(this)[0]
等同于 JavaScript 的 this
。
因此,您正在寻找:
$(this)[0].scrollIntoView();
希望对您有所帮助! :)
关于javascript - 如何在 jQuery 语法下使用 scrollIntoView()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44789479/