I have a webpage with multiple DIV elements. If i use the jquery function SlideUp to hide one (which is towards the bottom of the page), the page automatically scrolls back to the top. How can I prevent this?
我有一个包含多个DIV元素的网页。如果我使用jquery函数SlideUp隐藏一个(朝向页面底部),页面会自动滚动回到顶部。我怎么能阻止这个?
To trigger the jquery function, I do something like this:
要触发jquery函数,我会这样做:
$(MyLink).click(function() {
MyDiv.slideUp("slow");
}
MyLink
being within MyDiv
which is somewhere in the middle of the page.
MyLink位于MyDiv中,位于页面中间的某个位置。
2 个解决方案
#1
How do you trigger the jquery function, via anchors? If so, have the function return false, otherwise the browser will redirect to the href-attribute, which would be '#' in this case.
如何通过锚点触发jquery函数?如果是这样,让函数返回false,否则浏览器将重定向到href-attribute,在这种情况下为'#'。
#2
What I did was what someone suggested (adding return false
) and it worked:
我做的是有人建议的(添加返回false)并且它有效:
$(MyLink).click(function() {
MyDiv.slideUp("slow");
return false;
}
#1
How do you trigger the jquery function, via anchors? If so, have the function return false, otherwise the browser will redirect to the href-attribute, which would be '#' in this case.
如何通过锚点触发jquery函数?如果是这样,让函数返回false,否则浏览器将重定向到href-attribute,在这种情况下为'#'。
#2
What I did was what someone suggested (adding return false
) and it worked:
我做的是有人建议的(添加返回false)并且它有效:
$(MyLink).click(function() {
MyDiv.slideUp("slow");
return false;
}