W3 验证错误,锚标记中的嵌套标记

W3 Validation error, nested tags in an anchor tag

我的页面中有以下 html 代码:

<a class="fancybox" href="best-price.php">
<div id="bestprice">
</div><!--visitorinfo-->
</a>

在验证器中,我收到以下消息:

<div id="bestprice">

The mentioned element is not allowed to appear in the context in which you've placed it; the other mentioned elements are the only ones that are both allowed there and can contain the element mentioned. This might mean that you need a containing element, or possibly that you've forgotten to close a previous element.

One possible cause for this message is that you have attempted to put a block-level element (such as "<p>" or "<table>") inside an inline element (such as "<a>", "<span>", or "<font>").

有什么想法吗?

对于 HTML5 之前的文档类型,您不能将 <div> 标签放入 <a> 标签内。但是你可以这样做。

<a class="fancybox" href="best-price.php">
   <span id="bestprice">
   </span>
</a>

4.01过渡: http://validator.w3.org/#validate_by_input

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><!-- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> -->
<head>
<title>test</title>
</head>
<body>
<a class="fancybox" href="best-price.php">
   <span id="bestprice">
   </span>
</a>
</body>
</html>

HTML5: http://validator.w3.org/#validate_by_input

<!DOCTYPE html>
<head>
<title>test</title>
</head>
<body>
<a class="fancybox" href="best-price.php">
    <span id="bestprice">
    </span>
</a>
</body>
</html>

注意:正如 Rob 在评论中提到的那样。这个,

<a class="fancybox" href="best-price.php">
    <div id="bestprice">
    </div>
</a>

将在 HTML5 中验证,但不会在旧文档类型中验证。

<span>代替<div>

<a class="fancybox" href="best-price.php">
    <span id="bestprice">
        blabla
    </span><!--visitorinfo-->
</a>

并添加 CSS,使其表现得像块元素:

#bestprice {
    display: block;
}