链接到href =“#"与jquery slidetoggle一起使用时,将页面滚动到顶部 [英] link with href="#" scrolls page to top when used with jquery slidetoggle
问题描述
Possible Duplicate:
How do I stop a web page from scrolling to the top when a link is clicked that triggers javascript?
我正在使用jquery的slidetoggle来显示/隐藏div. 控制滑动的元素是文本链接(< \ a>中的一些文本) 具有href =#",因此它看起来像一个链接(下划线,更改光标).
I'm using jquery's slidetoggle to show/hide divs. the element that controls the sliding is a text link ( some text inside <\a>) which has href="#" so it will look like a link (underline, cursor change).
问题在于,当点击链接时,除了滑动效果外, 页面滚动到顶部.
the problem is that when the link is clicked, in addition to the sliding effect, the page scrolls to top.
我尝试用href ="替换href =#",但是禁用了div显示/隐藏效果. 我想我可以将其添加到标签Name ="somename"中,然后将href设置为href =#somename" 但我宁愿不使用这样的技巧.
i tried replacing href="#" with href="" but that disables the div show/hide effect. i guess i could add to the tag Name="somename" and then set the href to href="#somename" but i would rather not use tricks like that.
为什么href =#"将页面滚动到顶部?
why is href="#" scrolling the page to its top?
任何想法都将受到高度赞赏
any ideas would be highly appreciated
推荐答案
几个选项:
- 将
return false;
放在点击处理程序底部,并且href不会被遵循. - 将href更改为
javascript:void(0);
-
在处理程序中的事件上调用
preventDefault
:
- Put
return false;
at the bottom of your click handler and the href wont be followed. - Change the href to
javascript:void(0);
Call
preventDefault
on the event in your handler:
function( e ) {
e.preventDefault();
// your event handling code
}
这篇关于链接到href =“#"与jquery slidetoggle一起使用时,将页面滚动到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!