Html 没有 href 属性的锚标记安全吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5292343/
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
Is an anchor tag without the href attribute safe?
提问by john
Is it okay to use an anchor tag without including the href
attribute, and instead using a JavaScript click event handler? So I would omit the href
completely, not even have it empty (href=""
).
是否可以使用不包含href
属性的锚标记,而是使用 JavaScript 单击事件处理程序?所以我会href
完全省略,甚至不把它留空(href=""
)。
回答by dthrasher
In HTML5, using an a
element without an href
attribute is valid. It is considered to be a "placeholder hyperlink."
在 HTML5 中,使用a
没有href
属性的元素是有效的。它被认为是“占位符超链接”。
Example:
例子:
<a>previous</a>
Look for "placeholder hyperlink" on the w3c anchor tag reference page: https://www.w3.org/TR/2016/REC-html51-20161101/textlevel-semantics.html#the-a-element.
在 w3c 锚标记参考页面上查找“占位符超链接”:https: //www.w3.org/TR/2016/REC-html51-20161101/textlevel-semantics.html#the-a-element。
And it is also mentioned on the wiki here: https://www.w3.org/wiki/Elements/a
在这里的 wiki 上也提到了这一点:https: //www.w3.org/wiki/Elements/a
A placeholder link is for cases where you want to use an anchor element, but not have it navigate anywhere. This comes in handy for marking up the current page in a navigation menu or breadcrumb trail. (The old approach would have been to either use a span tag or an anchor tag with a class named "active" or "current" to style it and JavaScript to cancel navigation.)
占位符链接适用于您想要使用锚元素但不让它在任何地方导航的情况。这对于在导航菜单或面包屑路径中标记当前页面非常方便。(旧的方法是使用 span 标签或带有名为“active”或“current”的类的锚标签来设置样式,使用 JavaScript 来取消导航。)
A placeholder link is also useful in cases where you want to dynamically set the destination of the link via JavaScript at runtime. You simply set the value of the href attribute, and the anchor tag becomes clickable.
如果您希望在运行时通过 JavaScript 动态设置链接的目标,占位符链接也很有用。您只需设置 href 属性的值,锚标记就会变得可点击。
See also:
也可以看看:
回答by Gunnar Hoffman
It is OK, but at the same time can cause some browsers to become slow.
没关系,但同时会导致某些浏览器变慢。
http://webdesignfan.com/yslow-tutorial-part-2-of-3-reducing-server-calls/
http://webdesignfan.com/yslow-tutorial-part-2-of-3-reducing-server-calls/
My advice is use <a href="#"></a>
我的建议是使用 <a href="#"></a>
If you're using JQuery remember to also use:
如果您正在使用 JQuery,请记住还使用:
.click(function(event){
event.preventDefault();
// Click code here...
});
回答by Quentin
Short answer: No.
简短的回答:没有。
Long answer:
长答案:
First, without an href attribute, it will not be a link. If it isn't a link then it wont be keyboard (or breath switch, or various other not pointer based input device) accessible (unless you use HTML 5 features of tabindex
which are not universally supported). It is very rare that it is appropriate for a control to not have keyboard access.
首先,如果没有 href 属性,它将不是链接。如果它不是链接,则它不会是可访问的键盘(或呼吸开关,或各种其他非基于指针的输入设备)(除非您使用 HTML 5 的功能,tabindex
这些功能并非普遍支持)。控件没有键盘访问权限是非常罕见的。
Second. You should have an alternative for when the JavaScript does not run (because it was slow to load from the server, an Internet connection was dropped (e.g. mobile signal on a moving train), JS is turned off, etc, etc).
第二。当 JavaScript 不运行时,您应该有一个替代方案(因为从服务器加载速度很慢、互联网连接中断(例如移动火车上的移动信号)、JS 关闭等)。
Make use of progressive enhancementby unobtrusive JS.
回答by Rasive
If you have to use href for backwards compability, you can also use
如果必须使用 href 向后兼容,也可以使用
<a href="javascript:void(0)">link</a>
instead of # ,if you don't want to use the attribute
而不是 # ,如果你不想使用属性
回答by monokrome
The tag is fine to use without an href attribute. Contrary to many of the answers here, there are actually standard reasons for creating an anchor when there is no href. Semantically, "a" means an anchor or a link. If you use it for anything following that meaning, then you are fine.
该标签可以在没有 href 属性的情况下使用。与这里的许多答案相反,在没有 href 时创建锚点实际上是有标准原因的。从语义上讲,“a”表示锚点或链接。如果您将它用于遵循该含义的任何内容,那么您就可以了。
One standard use of the a tag without an href is to create named links. This allows you to use an anchor with name=blah and later on you can create an anchor with href=#blah to link to the named section of the current page. However, this has been deprecated because you can also use IDs in the same manner. As an example, you could use a header tag with id=header and later you could have an anchor pointing to href=#header.
不带 href 的 a 标签的一种标准用法是创建命名链接。这允许您使用 name=blah 的锚点,稍后您可以创建一个带有 href=#blah 的锚点以链接到当前页面的命名部分。但是,这已被弃用,因为您也可以以相同的方式使用 ID。例如,您可以使用带有 id=header 的标题标签,然后您可以使用一个指向 href=#header 的锚点。
My point, however, is not to suggest using the name property. Only to provide one use case where you don't need an href, and therefore reasoning why it is not required.
然而,我的观点并不是建议使用 name 属性。仅提供一个不需要 href 的用例,因此说明不需要它的原因。
回答by user3862605
From an accessibility perspective <a>
without a href is not tab-able, all links should be tab-able so add a tabindex='0" if you don't have a href.
从可访问性的角度来看,<a>
没有 href 的不是制表符,所有链接都应该是制表符,因此如果您没有 href,请添加 tabindex='0"。
回答by Jon
The <a>
tag without the "href" can be handy when using multi-level menus and you need to expand the next level but don't want that menu label to be an active link. I have never had any issues using it that way.
<a>
使用多级菜单时,没有“href”的标签会很方便,您需要展开下一级但不希望该菜单标签成为活动链接。我从来没有以这种方式使用它的任何问题。
回答by shashwat13
In some browsers you will face problems if you are not giving an href attribute. I suggest you to write your code something like this:
在某些浏览器中,如果您不提供 href 属性,则会遇到问题。我建议你写你的代码是这样的:
<a href="#" onclick="yourcode();return false;">Link</a>
you can replace yourcode() with your own function or logic,but do remember to add return false; statement at the end.
您可以用您自己的函数或逻辑替换 yourcode(),但请记住添加 return false;最后声明。