我有一个带有一些部分的文档,其中包含需要根据URL定位点收集的信息。jQuery - 从更改页面位置停止网址定位点
的什么,我试图做例子:
URL
http://mywebsite.com/page.php#section-2
HTML
<div id='information'>Data should be here!</div>
<div id='section-1'>This is data from section 1!</div>
<div id='section-2'>This is data from section 2!</div>
<div id='section-3'>This is data from section 3!</div>
<div id='section-4'>This is data from section 4!</div>
CSS
div{
height: 500px;
}
jQuery的
var hash = window.location.hash;
if(!hash == ""){
var data = $(hash).text();
$("#information").text(data);
}
但是当我加载URL,jQuery的工作正常,但页面跳转到#section-2
。
如何阻止这种情况发生?
在此先感谢。
我想你误解了这个问题。我不需要帮助收集数据,我需要它来阻止页面跳转到锚点。 – Kain 2014-12-07 08:58:22
看到编辑,也试过了。它仍然跳到锚点。 – Kain 2014-12-07 09:05:50
您是否在'$(document).ready()'中放置了'scrollTop(0)'? – MH2K9 2014-12-07 09:07:03