"的javascript:;"用于HTML锚标记中的href属性 [英] "javascript:;" for href attribute in HTML anchor tag
问题描述
更新
以下代码意味着什么?它需要JavaScript来工作?
< a href =javascript:;> Do Somthing< / a>
更新
等于低于?:
< a href => Do Somthing< / a>
使用javascript:作为href的开始属性链接告诉JavaScript引擎使用字符串的其余部分被解释为JavaScript。在这种情况下,严格解释会导致语法错误,因为这实际上是一个带有分号结束的空javascript线。像这样:
;
然而,大多数浏览器不会抛出错误,因为链接上的JavaScript是旧的语法,应该是尽可能避免。您可以安全地使用它作为一个什么都不做的链接,但我不会推荐它。
如果你想要一个链接什么都不做,你可以用它来代替:
< a href =#>连结< / a>
< a href =javascript:void(0);>连结< / a>
< a href =javascript:return false;>连结< / a>
使用空href字符串将使浏览器将其解释为相对链接。不以协议或标识符(如高级域或IP地址)开头的URL将被视为相对链接。例如,域google.com
上的链接index.htm
将创建链接 google.com/index.htm
。同样,href字符串会创建链接
google.com /
,所以空href字符串将导致浏览器导航到新页面。
通常情况下,链接不会显示指针光标,也不会像链接那样格式化元素(如果未指定href属性,因此您可以将它用作锚点元素,您可以将其链接到在URL中使用哈希字符。例如http://google.com/#an_anchor
会将您带到类似以下的锚点:< a id =an_anchor >这是一个锚< / a>
然而,您可以使用CSS强制链接被格式化,如下所示:
CSS:
a {
颜色:#00c ;
text-decoration:下划线;
光标:指针;
}
HTML:
< a>这是一个链接。< / a>
示例: http: //jsfiddle.net/J3RfH/
Update
What does the code below mean and do? It needs JavaScript to work?
<a href="javascript:;">Do Somthing</a>
Update
Is equal to below?:
<a href="">Do Somthing</a>
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:
;
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.
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.
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>
However you can use CSS to force the link to be formatted, like this:
CSS:
a {
color: #00c;
text-decoration: underline;
cursor: pointer;
}
HTML:
<a>This is a link.</a>
Example: http://jsfiddle.net/J3RfH/
这篇关于"的javascript:;"用于HTML锚标记中的href属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!