Html 我如何制作一个无处可去的链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1830927/
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-08-29 01:27:03  来源:igfitidea点击:

How do I make a link that goes no where

htmlhyperlink

提问by Ankur

Is it possible to make a link that does nothing. I want the item to be a link so I get the cursor to change on mouseover, but I don't want the link to actually go anywhere as it actually triggers the showing of a div with extra functionality on the same page.

是否可以制作一个什么都不做的链接。我希望该项目是一个链接,因此我可以在鼠标悬停时更改光标,但我不希望链接实际上转到任何地方,因为它实际上会触发在同一页面上显示具有额外功能的 div。

回答by leepowers

Will add to the browser history:

将添加到浏览器历史记录:

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

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

Won't add to the browser history (preferred method):

不会添加到浏览器历史记录(首选方法):

<a href="javascript:;"></a>

<a href="javascript:;"></a>

回答by jheddings

Instead, you could use a <span>and set the cursor style:

相反,您可以使用 a<span>并设置光标样式:

<span id="node42" class="node-link">My Text</span>

And specify the cursor in your stylesheet:

并在样式表中指定光标:

.node-link { cursor: pointer; }

This would also allow you to attach to the node for your actions later (show here using Prototype):

这也将允许您稍后附加到节点以进行操作(此处使用Prototype显示):

<script type="text/javascript">
    $('node42').observe('click', function(event) {
      alert('my click handler');
    });
</script>

回答by pstanton

in my opinion, the most complete hack free solution:

在我看来,最完整的无黑客解决方案:

<a href="" onclick="return false;">do absolutely nothing</a>

回答by Doug Neiner

Just add the style cursor:pointerto the element:

只需将样式添加cursor:pointer到元素:

<a id="clickme">Click Me</a>

CSS:

CSS:

#clickme { cursor: pointer }

Or (heaven forbid) in-line:

或(天堂禁止)内联:

<a style="cursor:pointer">Click Me</a>

回答by Mystical

Here are 2 classic javascriptapproaches that won't give you an errorin your JS-consoleand/or don't change the URL nor save it to your browser history:

这里有 2 种经典的 javascript方法,它们不会在您的JS 控制台中出现错误和/或不会更改 URL,也不会将其保存到浏览器历史记录中:

<a href="javascript:void(0);">Click on this javaScript error-free link that won't change your URL nor save it into your browser history</a><hr />
<a href="#void">Maybe a more readable approach, but unfortunately this one changes your URL</a>

回答by Mystical

If you just want style, set the the CSS cursor propertyto pointer.

如果您只想要样式,请将 CSS光标属性设置为指针。

But it sounds like you want <a href="javascript:void(do_something())">.

但听起来你想要<a href="javascript:void(do_something())">

回答by eglasius

One option would be to point to #someid - it'll move it vertical scroll to that element, but that may or not be what you want.

一种选择是指向#someid - 它会将其垂直滚动到该元素,但这可能是您想要的,也可能不是。

Ps. you can achieve the same result with styles.

附言。您可以使用样式实现相同的结果。

回答by Miguel Madero

I was looking for something simpler, and I think I found it. Do you want your link not to go anywhere? Omit the href.

我一直在寻找更简单的东西,我想我找到了。你想让你的链接不去任何地方吗?省略href.

<a class='irrelevant_for_this_example'>Some text</a>

<a class='irrelevant_for_this_example'>Some text</a>

href="#"will scroll to the top of the page.

href="#"将滚动到页面顶部。

href="javascript:;"seems dodgy.

href="javascript:;"似乎狡猾。

A JS solution seems unnecessary for something as simple.

对于这么简单的事情,JS 解决方案似乎没有必要。