jQuery scrollTop无法在本地工作

时间:2014-03-31 17:24:53

标签: jquery

我把代码简化为以下几行:

<script type='text/javascript' src='//code.jquery.com/jquery-1.9.1.js'></script>
<script>

$(function(){
 var stop = $("#gettingstarted").offset().top;
 var delay = 1000;
 $('html').animate({scrollTop: stop}, delay);
});
</script>
...
<h3 id="gettingstarted">Getting Started</h3>

然而它在本地不起作用。你可以在这里查看jsfiddle:http://jsfiddle.net/3Ez9Q/哪个有效。

Firefox 26.0。此外,我知道在所有浏览器中都应该使用$('html,body'),但我只是想让它在Firefox中运行。 jQuery正在工作:我可以使用jQuery库做其他事情。

编辑:好吧,我添加了jQuery mobile(1.3.0-beta),它使它工作。没有我在哪里读过我必须拥有它。奇怪。

无论如何,谢谢!

1 个答案:

答案 0 :(得分:3)

将https添加到此行

<script type='text/javascript' src='https://code.jquery.com/jquery-1.9.1.js'></script>

当您从file://C:/some/file.html而非http://localhost/some/site本地运行时,它会尝试加载file://code.jquery.com/jquery-1.9.1.js。常见的错误。

您可以在本地保存jquery文件并设置正确的相对路径,但是除非您也提供jquery文件,否则您必须更改它以进行部署,但最好从他们自己的CDN调用外部资源,你已经在做什么了。

最好在本地而不是http://使用file://