为什么没有表 >t >td 在使用子选择器时工作吗? [英] Why doesn't table > tr > td work when using the child selector?

查看:10
本文介绍了为什么没有表 >t >td 在使用子选择器时工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不明白为什么以下选择器按预期工作(即获得 td):

I can't really get why the following selector works as expected (i.e. get the td):

table tr td

但这个没有:

table > tr > td

tdtr 的后代,而 tr 又是 table 的后代,但它们也是彼此的子代.因此,我认为 > 选择器也可以工作.

The td is a descendant of tr, which in turn is a descendant of table, but they are also children of each other. Therefore, I thought that the > selector would work too.

我做了两个小提琴:

  1. 孩子:http://jsfiddle.net/brLee/
  2. 后代:http://jsfiddle.net/brLee/1/

为什么 > 选择器在这里不起作用?

Why isn't the > selector working here?

推荐答案

在 HTML 中,浏览器隐式地添加一个 tbody 元素,其中包含 tr 元素1,所以实际上,tr 永远不是table 的孩子.

In HTML, browsers implicitly add a tbody element within which to contain the tr elements1, so in reality, tr is never a child of table.

因此,您必须这样做:

table > tbody > tr > td

当然,如果您自己添加 tbody 元素,则使用相同的选择器.规范 解释了 tbody 是隐式添加的,否则:

Of course, if you add a tbody element yourself, you use the same selector. The spec explains when a tbody is added implicitly otherwise:

如果 tbody 元素中的第一个元素是 tr 元素,并且该元素是前面没有 tbody theadtfoot 元素,其结束标记已被省略.

Tag omission

A tbody element's start tag may be omitted if the first thing inside the tbody element is a tr element, and if the element is not immediately preceded by a tbody thead, or tfoot element whose end tag has been omitted.


1 对于正确用作 application/xhtml+xml 的 XHTML 文档而言,情况并非如此,但是,鉴于其 XML 根源.


1 This is not the case for XHTML documents that are properly served as application/xhtml+xml, however, given its XML roots.

这篇关于为什么没有表 >t >td 在使用子选择器时工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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