没有 href 属性的锚标记安全吗? [英] Is an anchor tag without the href attribute safe?

查看:34
本文介绍了没有 href 属性的锚标记安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用不包含 href 属性的锚标记,而是使用 JavaScript 单击事件处理程序?所以我会完全省略 href,甚至不让它为空(href="").

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="").

推荐答案

在 HTML5 中,使用没有 href 属性的 a 元素是有效的.它被认为是一个占位符超链接".

In HTML5, using an a element without an href attribute is valid. It is considered to be a "placeholder hyperlink."

示例:

<a>previous</a>

在 w3c 锚标记参考页面上查找占位符超链接":https://www.w3.org/TR/2016/REC-html51-20161101/textlevel-semantics.html#the-a-element.

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.

这里的 wiki 上也提到了这一点:https://www.w3.org/wiki/Elements/a

And it is also mentioned on the wiki here: https://www.w3.org/wiki/Elements/a

占位符链接适用于您想要使用锚元素但不让它在任何地方导航的情况.这对于在导航菜单或面包屑路径中标记当前页面非常方便.(旧的方法是使用 span 标签或带有名为active"或current"的类的锚标签来设置样式,使用 JavaScript 来取消导航.)

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.)

如果您想在运行时通过 JavaScript 动态设置链接的目的地,占位符链接也很有用.您只需设置 href 属性的值,锚标记就会变得可点击.

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.

另见:

这篇关于没有 href 属性的锚标记安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆