我在使用document.location.hash时能够滚动到感兴趣的特定DIV。 但是,当我刷新页面时,我无法将此滚动到相同的DIV。
JS
document.location.hash = 'sendEmailWrap';链接:
myDomain.com/publication.php?article=658#sendEmailWrap刷新页面时,链接末尾会显示“#sendEmailWrap”,因此滚动到同一个DIV不起作用。 我该如何获取链接
myDomain.com/publication.php?article=658I able to scroll to a particular DIV of interest when using document.location.hash. However, when I refresh the page I am unable to get this scrolling to the same DIV.
JS
document.location.hash = 'sendEmailWrap';Link:
myDomain.com/publication.php?article=658#sendEmailWrapWhen I refresh the page the '#sendEmailWrap' persists at the end of the link, as a result the scroll to the same DIV does not work. How do I go about getting the link back to
myDomain.com/publication.php?article=658最满意答案
如果由于您的内容在页面加载时尚未呈现而无法正常工作,则可以在内容准备好后“重置”哈希。 你可以尝试:
window.onload = function() { var hash = location.hash; location.hash = ''; location.hash = hash; };或者,在页面加载时简单地删除它:
window.onload = function() { location.hash = ''; };If it's not working due to your content not being rendered yet when the page loads, you could "reset" the hash after the content is ready. You could try:
window.onload = function() { var hash = location.hash; location.hash = ''; location.hash = hash; };or, to simply get rid of it when the page is loaded:
window.onload = function() { location.hash = ''; };更多推荐
发布评论