简体   繁体   English

迷路结束标签img

[英]Stray end tag img

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 ? 我们怎么能避免呢?

If your document is XHTML compliant then you have to close img tag with <img src="image.jpg"/> , not with <img>...</img> . 如果您的文档符合XHTML,则必须使用<img src="image.jpg"/>关闭img标记,而不是使用<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 用于HTML5和<!DOCTYPE HTML>

<!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"> for XHTML 1.0 Transitional

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: 更多阅读:

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" />

As such, “Stray end tag...” means just that an end tag is not allowed in the context where it appears. 因此,“Stray end tag ...”意味着在它出现的上下文中不允许结束标记。 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.” 在后一种情况下,一旦你解决了原来的问题,这个错误就会消失。“

From the symptoms (the error message string), we can infer that you are validating against HTML5 in HTML serialization. 从症状(错误消息字符串),我们可以推断您正在HTML序列化中对HTML5进行验证。 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”). 这意味着img元素不允许结束标记,因为开始标记也被视为关闭元素(“隐式闭合元素”)。

Thus, the solution is to either remove the </img> tag or to validate against HTML5 in XHTML serialization. 因此,解决方案是删除</img>标记或在XHTML序列化中对HTML5进行验证。 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. 后者对于网页不实用,但如果您将HTML用于其他内容,则应通过URL进行验证,引用以XML内容类型提供的资源。

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

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