"javascript:;" 对于 HTML 锚标记中的 href 属性

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

"javascript:;" for href attribute in HTML anchor tag

javascripthtml

提问by Pingpong

Update

更新

What does the code below mean and do? It needs JavaScript to work?

下面的代码是什么意思和做什么?它需要 JavaScript 才能工作?

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

Update

更新

Is equal to below?:

是否等于以下?:

<a href="">Do Somthing</a>

回答by Nimphious

Using "javascript:" as the start of a href attribute to a link tells the javascript engine to use the rest of the string to be interpreted as javascript. In this case, it would cause a syntax error in strict interpretation, since this is effectively an empty javascript line with a closing semicolon. Like this:

使用“javascript:”作为链接的 href 属性的开头告诉 javascript 引擎使用要解释为 javascript 的字符串的其余部分。在这种情况下,它会导致严格解释中的语法错误,因为这实际上是一个带有结束分号的空 JavaScript 行。像这样:

;

Most browsers won't throw an error, however, as javascript on links are old syntax and should be avoided when possible. You could safely use it as a link that does nothing, however I wouldn't recommend it.

然而,大多数浏览器不会抛出错误,因为链接上的 javascript 是旧语法,应尽可能避免。您可以安全地将它用作什么都不做的链接,但是我不推荐它。

If you want a link to do nothing, you could use this instead:

如果你想要一个链接什么都不做,你可以使用这个:

<a href="#">Link</a>
<a href="javascript:void(0);">Link</a>
<a href="javascript:return false;">Link</a>

Using an empty href string will make the browser interpret it as a relative link. URLs that don't start with a protocol or an identifier like a high level domain or IP address will be treated as relative links. For example, the link "index.htm"on the domain "google.com"will create the link "google.com/index.htm". In the same way, the href string ""will create the link "google.com/"and so empty href strings will cause the browser to navigate to a new page.

使用空的 href 字符串将使浏览器将其解释为相对链接。不以协议或标识符(如高级域或 IP 地址)开头的 URL 将被视为相对链接。例如,"index.htm"域上的链接"google.com"将创建链接"google.com/index.htm"。同样,href 字符串""将创建链接"google.com/",因此空的 href 字符串将导致浏览器导航到新页面。

Normally a link will not show the pointer cursor or format the element like a link if you do not specify a href attribute, this is so you can use it as an "anchor" element that you can link to using the hash character in a URL. Such as "http://google.com/#an_anchor"will take you to an anchor similar to this: <a id="an_anchor">This is an anchor</a>

通常,如果您不指定 href 属性,链接将不会显示指针光标或将元素格式化为链接,这样您就可以将其用作“锚”元素,您可以使用 URL 中的哈希字符链接到该元素. 例如"http://google.com/#an_anchor"会带你到一个类似这样的锚点:<a id="an_anchor">This is an anchor</a>

However you can use CSS to force the link to be formatted, like this:

但是,您可以使用 CSS 来强制格式化链接,如下所示:

CSS:

CSS:

a {
    color: #00c;
    text-decoration: underline;
    cursor: pointer;
}

HTML:

HTML:

<a>This is a link.</a>

Example: http://jsfiddle.net/J3RfH/

示例:http: //jsfiddle.net/J3RfH/

回答by ThiefMaster

It is a no-op.

这是一个空操作。

The other common way is href="#"but that requires you to return falsein the onclick event to avoid jumping to the top of the page and getting a #in the address bar.

另一种常见的方法是,href="#"但这要求您return false在 onclick 事件中避免跳转到页面顶部并#在地址栏中获取 a 。

Note that it is usually a good idea to have a link work both with and without JavaScript, i.e. do something like <a href="/whatever" onclick="dowhatever(); return false;">so people without JavaScript will simply open the page in the classic way while people with JavaScript get whatever nice stuff you did with JS.

请注意,让链接在使用和不使用 JavaScript 时都可以工作通常是一个好主意,即做一些类似的事情,<a href="/whatever" onclick="dowhatever(); return false;">这样没有 JavaScript 的人会简单地以经典方式打开页面,而有 JavaScript 的人则可以得到你用 JS 做的任何好东西。

If something isn't supposed to work without JavaScript at all, i.e. there is no useful hrefvalue, consider not using the atag at all but use a spanwith a proper style (cursor:pointerand possibly underlined).

如果某些东西在没有 JavaScript 的情况下根本不应该工作,即没有有用的href价值,请考虑根本不使用a标签,而是使用span具有适当样式的 a(cursor:pointer可能带有下划线)。

回答by ThiefMaster

if you will specify some function after :then it will be called using <a>tag.But here you have not specified any function so nothing will happen

如果你在:之后指定一些函数,它将使用<a>标签调用它。但是这里你没有指定任何函数所以什么都不会发生

回答by Oleg V. Volkov

It does nothing and is meant to replace default behavior of atag to go somewhere with no-op, making link essentially unclickable.

它什么都不做,旨在替换a标签的默认行为,以无操作的方式去某个地方,使链接基本上无法点击。