没有tr的表格有效吗? [英] Is a table without tr valid?

查看:41
本文介绍了没有tr的表格有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果只有一行,则省略表中的tr元素是否是有效的HTML?如果无效,当前所有的浏览器都会按预期进行解释吗?

Is it valid HTML to omit the tr elements in a table if you have only a single row? If it isn't valid, do all current browsers interpret it as intended?

换句话说,是...

<table>
     <td>column 1</td>
     <td>column 2</td>
</table>

...的行为与此...

... behave the same as this ...

<table>
   <tr>
     <td>column 1</td>
     <td>column 2</td>
   </tr>
</table>

?

推荐答案

尽管按照规范,它不是有效的HTML,但是现代网络浏览器的行为并不严格,不会给用户带来更多可读性的输出,而不是垃圾内容.有时是开发人员的错误(没有指向您).

Although it's not a valid HTML as per the Spec, modern web browsers don't behave strictly and give a more readable output to the users instead of junk which is sometimes the mistake of the developer (not pointing to you).

无法确定确切有效的方法和无效无效的方法.另外,如果当前浏览器解析了一些错误的代码,也不能确保将来的版本中也能做到这一点.

What exactly works and what not, cannot be determined. Also if some wrong code is parsed by the browsers currently does not ensure if will do the same in future versions too.

检查以下有关 浏览器的错误容忍度的摘录来自HTMLRocks文章浏览器的工作原理:现代网络浏览器的幕后 .该链接还提供了一些有关纠错的示例.

Check the below excerpt about Browser's Error Tolerance from HTMLRocks article How Browsers Work: Behind the scenes of modern web browsers. The link has certain examples also about error corrections.

错误处理在浏览器中非常一致,但是非常惊人它尚未成为HTML规范的一部分.像书签和后退/前进按钮只是在浏览器中开发的功能这些年来.在上重复了已知的无效HTML构造许多网站,浏览器尝试以与以下方式一致的方式对其进行修复其他浏览器.

Error handling is quite consistent in browsers, but amazingly enough it hasn't been part of HTML specifications. Like bookmarking and back/forward buttons it's just something that developed in browsers over the years. There are known invalid HTML constructs repeated on many sites, and the browsers try to fix them in a way conformant with other browsers.

HTML5规范确实定义了其中一些要求.(WebKit在开始时的注释中很好地总结了这一点.HTML解析器类.)

The HTML5 specification does define some of these requirements. (WebKit summarizes this nicely in the comment at the beginning of the HTML parser class.)

解析器将标记化的输入解析到文档中,从而建立文档树.如果文档格式正确,则将其解析为简单

The parser parses tokenized input into the document, building up the document tree. If the document is well-formed, parsing it is straightforward.

不幸的是,我们必须处理许多HTML文档,格式正确,因此解析器必须容忍错误.

Unfortunately, we have to handle many HTML documents that are not well-formed, so the parser has to be tolerant about errors.

我们必须至少照顾以下错误情况:

We have to take care of at least the following error conditions:

  1. 明确禁止在某些外部元素中添加要添加的元素标签.在这种情况下,我们应该关闭所有标签,直到禁止该元素,然后再添加.

  1. The element being added is explicitly forbidden inside some outer tag. In this case we should close all tags up to the one which forbids the element, and add it afterwards.

我们不允许直接添加元素.可能是撰写文档的人忘记了两者之间的一些标签(或者之间的标记是可选的).情况可能就是这样以下标记:HTML HEAD BODY TBODY TR TD LI(我忘了吗?).

我们想在一个内联元素中添加一个block元素.关闭所有内联元素,直到下一个较高的block元素.

We want to add a block element inside an inline element. Close all inline elements up to the next higher block element.

如果这样做没有帮助,请关闭元素,直到允许我们添加元素-或忽略标签.

If this doesn't help, close elements until we are allowed to add the element–or ignore the tag.

您还可以查看w3c文章验证HTML>不同的浏览器以不同的方式解释无效的HTML

You can also check the w3c article Validating your HTML > Different browsers interpret invalid HTML differently

有效的HTML是您与浏览器的唯一合同制造商.HTML规范说明了应如何编写,以及他们应如何解读您的文件

Valid HTML is the only contract you have with the browser manufacturers. The HTML specification says how you should write it, and how they should interpret your document

....

所有浏览器的行为均不正确;他们都是尝试填补您不正确的代码的空白.底线是,尽可能避免在页面中添加无效的标记!

None of the different browsers’ behaviours is incorrect; they’re all trying to fill in the gaps of your incorrect code. The bottom line is, avoid invalid markup if at all possible in your page!

请注意,HTML5修复了此问题,这是有史以来的第一次HTML定义了浏览器应如何处理格式错误的标记.在在撰写本文时,尚不支持此HTML5错误处理跨浏览器广泛传播,因此您还不能依赖它.

Note that HTML5 fixes this, as for the first time in the history of HTML it defines how browsers should handle badly-formed markup. At the time of writing however, support for this HTML5 error handling was not widespread across browsers, so you can't yet rely on it.

这篇关于没有tr的表格有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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