为什么使用 `javascript:void(0)` 而不是 `javascript:` 作为 href 什么都不做占位符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5237105/
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
Why use `javascript:void(0)` instead of `javascript:` as an href do nothing placeholder?
提问by Bryan Field
I have seen href="javascript:void(0)"
and I have seen href="javascript:;"
Is there any reason I would not just use href="javascript:"
instead?
我已经看到href="javascript:void(0)"
并且我已经看到href="javascript:;"
有什么理由我不只是使用它href="javascript:"
?
Edit:Let me make it clear: I am combining this with an onclick
and have no objection to using return false
if it becomes necessary to use the alternative. Of course this is only if the alternative makes better sense over javascript:
.
编辑:让我说清楚:如果有必要使用替代方案,我将其与 an 结合使用,onclick
并且不反对使用return false
。当然,这只有在替代方案比javascript:
.
Also, I have yet to see a answer to my question shown (clearly I think) in the first paragraph.Thanks, david. :)
此外,我还没有看到第一段中显示的问题的答案(显然我认为)。谢谢,大卫。:)
I have seen
href="javascript:void(0)"
and I have seenhref="javascript:;"
Is there any reason I would not just usehref="javascript:"
instead?
我已经看到
href="javascript:void(0)"
并且我已经看到href="javascript:;"
有什么理由我不只是使用它href="javascript:"
?
采纳答案by david
Doesn't answer you question but may shed a bit more light on it, some early versions of browsers like netscape had problems when a script was used within the href
.
不会回答您的问题,但可能会对此有所了解,一些早期版本的浏览器(如 netscape)在href
.
The void
operator was pretty much to only way force the click to do nothing.
该void
操作是相当多的唯一出路力的点击什么也不做。
Now, with browsers properly implementing "pseudo URLs", you can safely just use javascript:;
.
现在,随着浏览器正确实现“伪 URL”,您可以安全地使用javascript:;
.
回答by Spudley
Personally I'd avoid using either. I'd have onclick='return false;'
instead, and just set href='#'
.
我个人会避免使用。我会onclick='return false;'
改为,然后设置href='#'
.
Why? Because if you put Javascript code in the href, then it appears in the browser's status bar when you hover over the link, just like any other URL. It just looks messy.
为什么?因为如果您将 Javascript 代码放在 href 中,那么当您将鼠标悬停在链接上时,它就会出现在浏览器的状态栏中,就像任何其他 URL 一样。它只是看起来很乱。
In fact, since it's going to be ignored anyway, you could put any URL you fancy into the href
, not just a hash. The hash is safest, just in case your user has Javascript disabled, but aside from that point, it could be anything.
事实上,由于无论如何它都会被忽略,您可以将任何您喜欢的 URL 放入 中href
,而不仅仅是散列。哈希是最安全的,以防万一您的用户禁用了 Javascript,但除此之外,它可以是任何东西。
But I have to point out: having an <a>
tag with the href going to a void Javascript code seems to rather defeat the point of having an <a>
tag in the first place. I can see how you'd want to enable and disable a link at runtime, but simply setting the href to void does seem somewhat pointless. If it isn't going to be a real link, why not just have a <span>
or some other tag. If you must make it look like a link, it's trivial to do that in CSS.
但我必须指出:将<a>
带有 href的标签转到无效的 Javascript 代码似乎反而会破坏<a>
首先拥有标签的意义。我可以看到您希望如何在运行时启用和禁用链接,但简单地将 href 设置为 void 似乎没有意义。如果它不会是一个真正的链接,为什么不只是有一个<span>
或其他一些标签。如果你必须让它看起来像一个链接,那么在 CSS 中做到这一点是微不足道的。
回答by Jason Miesionczek
The ideal situation is to not put any code on the element itself, but rather use javascript in either a script tag, or in an external file and bind the event handler there.
理想的情况是不在元素本身上放置任何代码,而是在脚本标记或外部文件中使用 javascript 并在那里绑定事件处理程序。
回答by Rian Schmits
It's better not to use either. The href
attribute indicates a url. It's better to use event handlers to get the desired behaviour, which is probably the onclick
event.
最好也不要使用。该href
属性表示一个 url。最好使用事件处理程序来获得所需的行为,这可能就是onclick
事件。
In this case the desired behaviour apparently is to do nothing. Firstly, why use an anchor tag at all when it doesn't link anywhere? Secondly, this can be handles by preventing event bubbling, by letting the onclick
event handler return false
. Then the href
can either point to #
, or better even to have it point to a url that more or less has the same effect as the event handler, so it will degrade gracefully when javascript is turned off.
在这种情况下,所需的行为显然是什么都不做。首先,当它没有链接到任何地方时,为什么要使用锚标记?其次,这可以通过让onclick
事件处理程序返回来防止事件冒泡来处理false
。然后href
可以指向#
,或者甚至可以更好地指向一个 url,该 url 或多或少与事件处理程序具有相同的效果,因此当 javascript 关闭时它会优雅地降级。