Html 杂散结束标记 img
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24116546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stray end tag img
提问by Midhun Murali
While validating markup through W3C validator service got this below error
通过 W3C 验证器服务验证标记时出现以下错误
Stray end tag img
杂散结束标记 img
Code is like below
代码如下
<a title="text" href="url">
<img class="text" src="imgSrc" alt="Text"></img>
</a>
What does it means ? How we can avoid it ?
这是什么意思 ?我们怎样才能避免呢?
回答by Bud Damyanov
If your document is XHTML compliant then you have to close img
tag with <img src="image.jpg"/>
, not with <img>...</img>
.
如果您的文档与 XHTML 兼容,那么您必须用 关闭img
标记<img src="image.jpg"/>
,而不是用<img>...</img>
。
If your document is HTML5 compliant, then you do not need the />
part, only use <img src="image.jpg">
如果您的文档符合 HTML5,那么您不需要该/>
部分,只需使用<img src="image.jpg">
And if you wonder what means that document should be XHTML or HTML5 compliant - this is the very first line of your HTML page, the so called document type definition
:
如果您想知道该文档应该与 XHTML 或 HTML5 兼容是什么意思 - 这是您的 HTML 页面的第一行,即所谓的document type definition
:
<!DOCTYPE HTML>
for HTML5 and
<!DOCTYPE HTML>
对于 HTML5 和
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
for XHTML 1.0 Transitional
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
用于 XHTML 1.0 过渡
NOTE:The <!DOCTYPE>
declaration is mandatory (if you want your pages to validate with an HTML validator) and should always be the first thing in an HTML document.
注:该<!DOCTYPE>
声明是强制性的(如果你希望你的网页来验证与HTML验证),应该永远是一个HTML文档中的第一件事情。
NOTE:Although a document type definition is technically not required for a functioning web page, it is good practice to always include it in your code. As you learn to build web pages, get into the habit of always including the document type definition in your code.
注意:尽管从技术上讲,功能性网页不需要文档类型定义,但始终将其包含在您的代码中是一种很好的做法。当您学习构建网页时,请养成始终在代码中包含文档类型定义的习惯。
More reading:
更多阅读:
回答by karllindmark
Basically, it means that you should remove the </img>
, as it's not needed for the <img>
tag:
基本上,这意味着您应该删除</img>
,因为<img>
标签不需要它:
<img class="text" src="imgSrc" alt="Text">
Alternatively, just for reference purposes, there's also the XHTML way of "closing" the tag:
或者,仅供参考,还有“关闭”标签的 XHTML 方式:
<img class="text" src="imgSrc" alt="Text" />
回答by Jukka K. Korpela
As such, “Stray end tag...” means just that an end tag is not allowed in the context where it appears. As the validator's explanation says: “The Validator found an end tag for the above element, but that element is not currently open. This is often caused by a leftover end tag from an element that was removed during editing, or by an implicitly closed element (if you have an error related to an element being used where it is not allowed, this is almost certainly the case). In the latter case this error will disappear as soon as you fix the original problem.”
因此,“Stray end tag...”意味着在出现的上下文中不允许使用结束标记。正如验证器的解释所说:“验证器找到了上述元素的结束标记,但该元素当前未打开。这通常是由在编辑期间删除的元素的剩余结束标记或隐式关闭元素引起的(如果您遇到与在不允许使用的元素相关的错误,则几乎可以肯定是这种情况)。在后一种情况下,一旦您修复了原始问题,此错误就会消失。”
From the symptoms (the error message string), we can infer that you are validating against HTML5 in HTML serialization. This means that no end tag is allowed for an img
element, since the start tag is treated as also closing the element (“implicitly closed element”).
根据症状(错误消息字符串),我们可以推断您正在针对 HTML 序列化中的 HTML5 进行验证。这意味着img
元素不允许使用结束标记,因为开始标记也被视为关闭元素(“隐式关闭元素”)。
Thus, the solution is to either remove the </img>
tag or to validate against HTML5 in XHTML serialization. The latter is not practical for web pages, but if you are using HTML for something else, you should validate by URL referring to a resource that is served with an XML content type.
因此,解决方案是删除</img>
标记或在 XHTML 序列化中针对 HTML5 进行验证。后者对于网页不实用,但如果您将 HTML 用于其他用途,您应该通过引用以 XML 内容类型提供的资源的 URL 进行验证。