简体   繁体   English

W3验证错误,锚标签中嵌套了标签

[英]W3 Validation error, nested tags in an anchor tag

I have the following html code in my page: 我的页面中有以下html代码:

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

At the validator I have the following message: 在验证器中,我收到以下消息:

<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> "). 出现此消息的可能原因是,您试图将一个块级元素(例如“ <p> ”或“ <table> ”)放在一个内联元素(例如“ <a> ”,“ <span> ”或“ <font> ”)。

Any ideas? 有任何想法吗?

You can't put a <div> tag inside of an <a> tag for doctypes prior to HTML5. 对于HTML5之前的文档类型,您不能在<a>标记内放置<div>标记。 But you could do something like this. 但是你可以做这样的事情。

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

4.01 transitional: http://validator.w3.org/#validate_by_input 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 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>

Note: As Rob mentioned in the comments. 注意:正如Rob在评论中所述。 This, 这个,

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

Will validate in HTML5, but not in the older doctypes. 将在HTML5中验证,但在较旧的文档类型中不验证。

Use <span> to replace <div> 使用<span>替换<div>

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

And add CSS, to make it acts like a block element: 并添加CSS,使其表现得像块元素:

#bestprice {
    display: block;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM