javascript 锚点点击并保持页面位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13524866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:02:41  来源:igfitidea点击:

Anchor click and keeping page position?

javascripthtml

提问by AliR?za Ad?yah?i

In my page there are some anchor that has no link, like this:

在我的页面中有一些没有链接的锚点,如下所示:

<a href="#">Anchor</a>

When I click anchor that is bottom of the page, Page is scrolling to top. I want to keep page position, if I clicked the link. How Can I do this. (no js is better for me)

当我单击页面底部的锚点时,页面正在滚动到顶部。我想保持页面位置,如果我点击链接。我怎样才能做到这一点。(没有js对我更好)

I hope, I could explain.

我希望,我可以解释一下。

Thanks in advance.

提前致谢。

回答by Blender

You'll still have to use JS, but you can do it inline:

您仍然必须使用 JS,但您可以内联进行:

<a href="javascript:void(0);">Test</a>???????????????????????????????????????????

Demo: http://jsfiddle.net/7ppZT/

演示:http: //jsfiddle.net/7ppZT/

Alternatively, you can capture the event:

或者,您可以捕获事件:

$('a[href^="#"]').click(function(e) {
    e.preventDefault();
});

回答by Ben

You can put anchors in anywhere in the page where you want it to go.

您可以将锚点放置在页面中您希望它去的任何位置。

<a href="#stophere">Anchor</a>

<a id='stophere'>Bottom Of Page</a>

Then, the link will go to the named anchor. Just stick in that element wherever you want to stop.

然后,链接将转到命名的锚点。只需在您想要停止的任何地方坚持该元素。

回答by Mahmoud Farahat

to fix this issue , I use this code , it's script solution but works on all browsers

为了解决这个问题,我使用了这个代码,它是脚本解决方案,但适用于所有浏览器

<a href="javascript:void(0)">Anchor</a>

回答by Habeeb Perwad

<a href="#noname">Anchor</a>

Make sure that nonameyou didn't use for idattribute of any tag or nameattribute of atag.

确保noname您没有使用id任何标签的name属性或标签的属性a

回答by phschoen

you have to set a name and add the name after the #like this

您必须设置一个名称并在#像这样的后面添加名称

<a name="adsf" href="#adsf">Anchor</a>