<a> tags that lack an href attribute?</a>是否存在任何浏览器问题

时间:2012-07-09 19:59:43

标签: html cross-browser w3c-validation

上下文:目前,如果锚点应该指向的链接为空,Wt(Web Toolkit)会将WAnchor对象呈现为<span>标记而不是<a>标记。

然而,这种方法存在一些问题,特别是它增加了CSS样式的不必要的复杂性(例如,当使用Twitter Bootstrap时)。

我参与了与Wt开发人员之间的讨论,提出了一个修改此行为的补丁:http://redmine.emweb.be/issues/1348

他指出,目前的行为是由于某些浏览器在没有<a>属性(可能是较旧的移动浏览器?)的情况下呈现href代码时出现问题而实现的。

根据官方HTML规范,这没有问题:

据我所知......

<a>Link</a> // OK
<a href>Link</a> or <a href="">Link</a> // Undefined? (but common behavior is to navigate to the current URL)
<a href="/items">Link</a> // OK

我还使用W3C HTML5验证器检查了以下文档:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<a>Example of an anchor with no href attribute.</a>
<a href>Example of an anchor with a null href attribute.</a>
<a href="">Example of an anchor with an empty string href attribute.</a>
<a href="http://www.google.com/">Example of an anchor that links to Google.</a>
</body>
</html>

我收到一个警告:

  

Line 9, Column 8: Attribute href without an explicit value seen. The attribute may be dropped by IE7. <a href>Example of an anchor with a null href attribute.</a>

所以我的问题是:是否有任何已知情况,href标记上的缺少<a>属性会导致某些浏览器出现问题? IE浏览器?移动Safari?

1 个答案:

答案 0 :(得分:2)

没有a属性的href元素在HTML中始终有效,并且没有理由期望任何浏览器遇到此问题。这样的元素实际上类似于span,即缺少任何语义。传统上<a name="...">...</a>已用于设置目标锚点,通常没有href属性,并且此类元素可在浏览器中使用,但它更现代且值得推荐使用id属性(在任何元素上。)

正确使用href="",并使用空URL,该URL被解释为同一文档引用。

根据当前的HTML规范,使用href没有任何值是无效的。通过HTML5草稿,它可用于HTML序列化,它等同于href=""。无法保证浏览器的行为方式。

相关问题