html - 为什么这个 html 代码有效?

标签 html validation

<分区>

谁能给我解释一下,为什么这段代码有效?

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
    <html<head>
    <title//
    <p ltr<span id=p></span</p>
    </>

期待您的回复。感谢关注

最佳答案

那些 HTML 代码是有效的,因为 HTML 4.01 允许这些东西,并且尊重 doctype 的 Internet 浏览器将毫无问题地显示它。

如果将 doctype 更改为 HTML 5,肯定会报错,因为 HTML 5 对编写正确的 HTML 标记更加严格。

HTML 4.01(根据 https://validator.w3.org/#validate_by_input没有错误):

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
    <html<head>
    <title//
    <p ltr<span id=p></span</p>
    </>

HTML 5(15 个错误 根据 https://validator.w3.org/#validate_by_input ):

 <!DOCTYPE html>
    <html<head>
    <title//
    <p ltr<span id=p></span</p>
    </>

解释:

该 HTML 结构是有效的,因为根据 HTML 4.0.1 规范 (https://www.w3.org/TR/1999/REC-html401-19991224/):

B.3.7 Shorthand markup

Some SGML SHORTTAG constructs save typing but add no expressive capability to the SGML application. Although these constructs technically introduce no ambiguity, they reduce the robustness of documents, especially when the language is enhanced to include new elements. Thus, while SHORTTAG constructs of SGML related to attributes are widely used and implemented, those related to elements are not. Documents that use them are conforming SGML documents, but are unlikely to work with many existing HTML tools.

The SHORTTAG constructs in question are the following:

  • NET tags:
    <name/.../
  • closed Start Tag:
    <name1<name2>
  • Empty Start Tag:
    <>
  • Empty End Tag:
    </>

引自https://www.w3.org/TR/1999/REC-html401-19991224/appendix/notes.html#h-B.3.7 .


因此,基于 HTML 4.01 规范,这意味着:

1     <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
2        <html<head>
3        <title//
4        <p ltr<span id=p></span</p>
5        </>
  • 第 1 行是 HTML 4.0.1 的有效文档类型。
  • 第 2 行有效 <html>打开标签,关闭 </html>标签不是必需的。
  • 第 2 行也有效 <head>打开标签,关闭 </head>标签不是必需的。
  • 第 3 行是有效的 <title>打开 Internet 浏览器读取的标签 <title// <p ltr<span id=p>就像 <title> , 关闭 </title>标签不是必需的。
  • 第4-5行是<title>的内容(内部HTML)标签是 </span</p> </> (这是互联网浏览器显示为页面标题的内容)。

这是我的补充说明。希望能帮到你。

关于html - 为什么这个 html 代码有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35600283/

相关文章:

html - 如何构建具有相同宽度的响应式 DIV 网格

javascript - Google CSE X 框架选项

java - 根据枚举验证 JSON 字段

php - 页面刷新后移动到网页的一部分

c# - 俄罗斯增值税号验证

javascript - 如何在网页上显示 moment.js 时间?

javascript - 谷歌地图 v3 出现扭曲并加倍

javascript - 如何检测输入字段上的编程值变化

ruby-on-rails - 使用 `update_all` 时运行验证

jquery - 我可以让 IE7、HTML5 表单验证和 jQuery .submit 事件一起工作吗