链接到href =“#"与jquery slidetoggle一起使用时,将页面滚动到顶部 [英] link with href="#" scrolls page to top when used with jquery slidetoggle

查看:110
本文介绍了链接到href =“#"与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

推荐答案

几个选项:

  1. return false;放在点击处理程序底部,并且href不会被遵循.
  2. 将href更改为javascript:void(0);
  3. 在处理程序中的事件上调用preventDefault:

  1. Put return false; at the bottom of your click handler and the href wont be followed.
  2. Change the href to javascript:void(0);
  3. Call preventDefault on the event in your handler:

function( e ) {
  e.preventDefault();
  // your event handling code
}

这篇关于链接到href =“#"与jquery slidetoggle一起使用时,将页面滚动到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆