使用JavaScript设置HTML页面的垂直位置 [英] Setting HTML page vertical position with JavaScript

查看:131
本文介绍了使用JavaScript设置HTML页面的垂直位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以上下滚动的HTML页面(虽然不多,但确实可以滚动).执行一些JavaScript后如何设置页面中的滚动位置?

I have a HTML page that scrolls up and down (not a lot, but it does scroll). How can I set the scroll position in the page after executing some JavaScript?

我正在使用jQuery在页面底部注入一些其他HTML,我想在添加新内容后以编程方式滚动到该位置.

I'm using jQuery to inject some additional HTML at the bottom of the page and I'd like to programmatically scroll to the position of that new content after it's added.

推荐答案

执行此操作的另一种方法,以便您可以选择:

Another way to do this, so that you have the option:

在您要添加到页面底部的HTML中,您可以插入一个命名的锚标记,然后更改URL,以便页面移至该页面(仅供参考:它不会刷新页面).

In the HTML you are adding to the bottom of the page, you can insert a named anchor tag and then change the URL so that the page moves there (FYI: it will not refresh the page).

// add HTML like this, dynamically:
// <a name="moveHere" />

// the javascript to make the page go to that location:
window.location.hash = "moveHere";

根据您正在执行的操作,这可能是有用的解决方案,也可能不是.

Depending on what you are doing, this may or may not be a useful solution.

这篇关于使用JavaScript设置HTML页面的垂直位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆