[英]Jquery triggering function after attr() does not work
What I am trying to achieve is that if someone clicks on the specified link in the menu, it redirects to the homepage and then scrolls to the specific content. 我要实现的目标是,如果有人单击菜单中的指定链接,它将重定向到主页,然后滚动到特定内容。
My Jquery code is this simple: 我的Jquery代码很简单:
$(document).ready(function () {
$("#upbar li:nth-of-type(2)").click(function(){
$(location).attr('href', 'index.html');
$('html,body').animate({
scrollTop: $("#samples").offset().top}, 700);
});
});
It redirects to the index.html page but it does not scroll :/ 它重定向到index.html页面,但不滚动:/
Thank you in advance! 先感谢您!
Martin 马丁
Assuming location
is targetting window.location
in your example to change browser URL... 假设
location
在您的示例中以window.location
为目标以更改浏览器URL ...
Changing the location
of the browser will load a new page and throw away the running jQuery/JavaScript. 更改浏览器的
location
将加载新页面并丢弃正在运行的jQuery / JavaScript。 $('html,body').animate(
will never run on the intended new page. $('html,body').animate(
永远不会在预期的新页面上运行。
eg 例如
$(location).attr('href', 'index.html#samples');
or better yet just: 甚至更好:
location.href = "index.html#samples";
Or you can load the page using Ajax, so your code stays resident 或者,您可以使用Ajax加载页面,从而使代码保持常驻
Or pass something to the new page to tell it where to scroll 或将一些内容传递到新页面以告知其滚动位置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.