防止页面加载时的默认哈希行为 [英] Prevent default hash behavior on page load
问题描述
我有一个带有选项卡式界面的页面.每个标签都有唯一的ID.我已经启用了指向指向该页面的链接,并在哈希后面添加了id.现在,我试图避开默认的浏览器行为,该行为会打开页面上元素所在位置带有哈希的URL.
I have a page with a tabbed interface. Each tab has a unique id. I've enabled links pointing to that page with the appended id after the hash, and I'm now trying to circumvent the default browser behavior that opens a URL with hash at the location of the element on the page.
所以:
- pageA链接到pageB,如下所示:
<a href="pageB.php#Tab4">Go</a>
- pageB打开,我的jQuery激活了正确的选项卡,但是浏览器已向下滚动到
<div id="Tab4">
在页面上的位置.
- pageA links to pageB like this:
<a href="pageB.php#Tab4">Go</a>
- pageB opens, and my jQuery activates the correct tab, but the browser has scrolled down to where
<div id="Tab4">
is located on the page.
这正是我要防止的事情.
That's exactly what I want to prevent.
有什么想法吗? 谢谢!
Any ideas? Thanks!
推荐答案
没有办法防止默认的哈希行为,但是您可以更改哈希方案,以使#tag与您的任何ID都不对应页.在页面加载时,获取哈希值并附加一个常量(例如:_hash
),然后使用jQuery滚动到该值.
There isn't a way to prevent the default hash behavior, but you can alter your hash scheme so the #tag doesn't correspond to any ID on your page. On page load, take the hash value and append a constant (ex: _hash
), then scroll to that using jQuery.
示例: http://mysite/page.php#tab4
page.php具有<div id="tab4_hash"></div>
page.php has <div id="tab4_hash"></div>
在页面加载时,通过执行tab4
+ _hash
On page load, get the div by doing tab4
+ _hash
这篇关于防止页面加载时的默认哈希行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!