浏览器会自动插入缺少的HTML标签吗? [英] Do browsers automatically insert missing HTML tags?

查看:188
本文介绍了浏览器会自动插入缺少的HTML标签吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用网页将内容从XML转换为HTML,然后将其显示在HTML模板页面上。我有一个页面,其中一些元素在模板中定义错误。我唯一能想到的就是XML的翻译不正确。现在我的问题是,如果翻译不正确并且缺少标签(例如缺少< / div> 标签),浏览器会自动插入必要的标签吗?



我只是不明白我的模板页面上的某个元素可能会出现在错误的位置,除非浏览器正在执行一些我不知道的事情......

解决方案

大多数情况下,未关闭标签会自动关闭 - 但每个浏览器都会以不同方式处理未关闭标签。它不仅取决于您的浏览器,它还取决于doctype规范。



引用 @ Elliott Frisch


我相信大多数网页浏览器会优雅地尝试失败(除非您使用
a严格的DTD),问题是规范不能定义如何
渲染无效内容


您可以在这里找到更多有关浏览器无效标记解释的信息:
http:// www.w3.org/wiki/Validating_your_HTML#Different_browsers_interpret_invalid_HTML_differently



根据W3C的说法:指定您的文档类型以及使用验证器可以帮助查找违规标签: http://www.w3.org/TR/WCAG2 0-TECHS / H74.html



以下是W3C.H74的一个提示:


示例1:HTML



HTML页面包含文档类型声明(有时称为
为!DOCTYPE语句)。开发人员可以使用离线或在线
验证程序(请参阅下面的参考资料)检查所有id属性值
是否唯一,并根据
规范使用开始和结束标记。



注意:标签需要关闭元素的规范有
随着HTML5的引入而发生变化



示例2:XHTML

与其他基于XML的文档一样,XHTML文档引用了
文档类型定义(DTD)或其他类型的XML模式。
开发人员可以使用在线或离线验证程序(包括验证编辑器中的
工具)来检查根据规范使用的开始和结束标记是



示例3:使用测试框架



当网站动态生成HTML或XHTML而不是仅提供静态页面
时,开发人员可以使用XHTMLUnit,XML Test Suite或
类似框架来测试生成的XHTML代码。


$ b W3C提供测试您的资源html: http://www.w3.org/TR/WCAG20-TECHS/ H74.html



整个网络上有许多HTML验证器,但正确指定doctype以充分利用它们非常重要。这是另一个资源列表,可以帮助你: http://www.w3.org/wiki/Validating_your_HTML


I'm working with a web page the translates content from XML to HTML and then displays it on the HTML template page. I have a single page where some elements are getting misplaced that are defined in the template. The only thing I can think of is that the XML is getting translated incorrectly. Now my question is, if it is translated incorrectly and there are missing tags (e.g. missing </div> tags), would a browser automatically insert the necessary tags?

I just don't understand how else an element on my template page could be in the wrong place unless the browser is doing something I don't know about...

解决方案

For the most part unclosed tags will be automatically closed - but each browser treats an unclosed tag differently. Not only does it depend on your browser, it also depends on the doctype spec.

To quote @Elliott Frisch :

I believe most web-browsers attempt to fail gracefully (unless you use a strict DTD), the issue is that the specification can't define how to render invalid content

You can find out more information about browser interpretation of invalid markup here: http://www.w3.org/wiki/Validating_your_HTML#Different_browsers_interpret_invalid_HTML_differently

According to W3C: Specifying your doctype along with using validators can help find offending tags: http://www.w3.org/TR/WCAG20-TECHS/H74.html

Here is an exerpt from W3C.H74 :

Example 1: HTML

HTML pages include a document type declaration (sometimes referred to as !DOCTYPE statement). The developer can use offline or online validators (see Resources below) to check that all id attribute values are unique and that opening and closing tags are used according to the specification.

Note: The specification for which tags require closing elements has changed with the introduction of HTML5.

Example 2: XHTML

Like other other XML-based documents, XHTML documents reference a Document Type Definition (DTD) or other type of XML schema. The developer can use online or offline validators (including validation tools built into editors) to check that opening and closing tags are used according to the specification.

Example 3: Using test frameworks

When a Website generates HTML or XHTML dynamically instead of serving only static pages, a developer can use XHTMLUnit, XML Test Suite or a similar framework to test the generated XHTML code.

W3C provides resources for testing your html: http://www.w3.org/TR/WCAG20-TECHS/H74.html

There a number of HTML validators across the web, but it is important to specify the doctype properly to leverage them all. Here is another resource list that may help you: http://www.w3.org/wiki/Validating_your_HTML

这篇关于浏览器会自动插入缺少的HTML标签吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆