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

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

问题描述

可以使用锚标签而不包含 href 属性,而使用JavaScript单击事件处理程序吗?所以我会完全省略 href ,甚至不会有空( href =)。

解决方案

在HTML5中,使用 a 元素而不需要 href 属性是有效的。它被认为是占位符超链接。


$ b $ p $ < a>上一个< / a>

在w3c锚点标记参考页面上查找占位符超链接: https://www.w3.org/TR/2016/ REC-html51-20161101 / textlevel-semantics.html#的-A-元件

它也在wiki上提到:
https://www.w3.org/wiki/Elements/a



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



如果您想要在运行时通过JavaScript动态设置链接的目的地,则占位符链接也很有用。您只需设置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="").

解决方案

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

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.

And it is also mentioned on the wiki here: 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.)

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.

See also:

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

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