- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
有没有办法选择匹配(或不匹配)任意选择器的第 n 个子元素?例如,我想选择每个奇数表行,但在行的子集中:
table.myClass tr.row:nth-child(odd) {
...
}
<table class="myClass">
<tr>
<td>Row
<tr class="row"> <!-- I want this -->
<td>Row
<tr class="row">
<td>Row
<tr class="row"> <!-- And this -->
<td>Row
</table>
但是 :nth-child()
似乎只是计算所有 tr
元素,不管它们是否属于“行”类,所以我结束了使用一个偶数“行”元素,而不是我正在寻找的两个。 :nth-of-type()
也会发生同样的事情。
谁能解释一下为什么?
最佳答案
这是一个非常常见的问题,由于对 :nth-child(An+B)
和 :nth-of-type()
工作方式的误解而产生.
在选择器级别 3 中,:nth-child()
pseudo-class计算同一父级下所有 sibling 中的元素。它不仅计算与选择器其余部分匹配的 sibling 。
同样,:nth-of-type()
pseudo-class计算共享相同元素类型的 sibling ,它指的是 HTML 中的标记名称,而不是选择器的其余部分。
这也意味着如果同一个父元素的所有子元素都是相同的元素类型,例如在表体的情况下,其唯一的子元素是 tr
元素,或者列表元素只有children 是 li
元素,然后 :nth-child()
和 :nth-of-type()
将表现相同,即对于每个值An+B 的 :nth-child(An+B)
和 :nth-of-type(An+B)
将匹配同一组元素。
事实上,给定复合选择器中的所有简单选择器,包括 :nth-child()
和 :not()
等伪类,都有效 彼此独立,而不是查看与选择器的其余部分匹配的元素的子集。
这也意味着在每个单独的复合选择器中的简单选择器之间没有顺序的概念1,这意味着例如以下两个选择器是等价的:
table.myClass tr.row:nth-child(odd)
table.myClass tr:nth-child(odd).row
翻译成英文,它们的意思都是:
Select any
tr
element that matches all of the following independent conditions:
- it is an odd-numbered child of its parent;
- it has the class "row"; and
- it is a descendant of a
table
element that has the class "myClass".
(您会注意到我在这里使用了无序列表,只是为了说明问题)
选择器级别 4 试图通过允许 :nth-child(An+B of S)
来纠正此限制。 2 接受任意选择器参数 S,这也是由于选择器如何在复合选择器中独立于另一个操作,正如现有选择器语法所规定的那样。所以在你的情况下,它看起来像这样:
table.myClass tr:nth-child(odd of .row)
当然,作为全新规范中的全新提案,这可能要到a few years down the road 才能看到实现。 .
与此同时,您必须使用脚本来过滤元素并相应地应用样式或额外的类名。例如,以下是使用 jQuery 的常见解决方法(假设表中只有一个行组填充了 tr
元素):
$('table.myClass').each(function() {
// Note that, confusingly, jQuery's filter pseudos are 0-indexed
// while CSS :nth-child() is 1-indexed
$('tr.row:even').addClass('odd');
});
配合相应的CSS:
table.myClass tr.row.odd {
...
}
如果您正在使用自动测试工具(如 Selenium)或使用 BeautifulSoup 等工具抓取 HTML,其中许多工具都允许使用 XPath 作为替代:
//table[contains(concat(' ', @class, ' '), ' myClass ')]//tr[contains(concat(' ', @class, ' '), ' row ')][position() mod 2)=1]
其他使用不同技术的解决方案留给读者作为练习;这只是一个简短的人为示例。
1 如果指定类型或通用选择器,它必须在前面。然而,这并没有改变选择器的基本工作方式。这只不过是一个句法怪癖。
2 这最初是作为 :nth-match()
提出的,但是因为它仍然只计算与其兄弟相关的元素,而不是每个元素与给定选择器匹配的其他元素,自 2014 年起,它已被重新用作现有 :nth-child()
的扩展。
关于css - 我可以合并 :nth-child() or :nth-of-type() with an arbitrary selector? 吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38420408/
我正在使用 QuickCheck 1,我有以下数据类型: data A = ... instance Arbitrary A where ... data B = ... instance Arbit
背景: Vim 有很多很棒的教程和“技巧”页面,但是非常困难的一件事是找到关于如何做一些可以在自己熟悉的文本编辑器 IDE 中轻松完成的任意事情的具体说明。因此,我要求逐步说明我将如何在 Vim 中执
我不确定正确的名称是什么,到目前为止我理解使用可变模板参数的概念(例如,与 std::initializer_list 相比)。 所以让我们假设有任意数量的类 k 和任意数量的参数 i 这取决于每个类
是否有任何 TypeScript 解决方案可以帮助我拥有“无限”数量的泛型类型,当然不必键入所有这些类型? 示例如下: type Args> = { component: T, presetProps
我有简单的元组(例如从数据库中读取),我不知道元素的数量和内容。例如。(String, Int, Int)或 (String, Float, String, Int) . 我想编写一个通用函数,它将采
rust quickcheck documentation注意对于任何实现 Arbitrary 的类型 They must also be sendable and static since ever
我有以下数据和函数 data Foo = A | B deriving (Show) foolist :: Maybe Foo -> [Foo] foolist Nothing = [A] fool
我尝试计算 math.exp(9500) 但遇到了 OverflowError: math range error(大约是 6.3e4125)。来自 this question似乎是由于 float
我想知道为什么需要任意,因为自动化属性测试需要属性定义,例如 val prop = forAll(v: T => check that property holds for v) 和值 v 生成器。用
@SuppressWarnings("unchecked") public static final Ordering ARBITRARY_ORDERING = (Ordering)Ordering.
我在替换“任意”定界符之间的文本文件部分时遇到一些问题。 这是我想要做的:我有如下标记: // [ADX START] ANYTHING // [ADX END] ANYTHING /*
据我所知,在 HTML5 中组成标签名称是完全合法的,并且它们可以正常使用 CSS 样式和嵌套等。 当然,我的任意标记名称对不理解它们的浏览器没有任何影响,但它大大提高了我的代码的可读性,从而更易于维
这个问题在这里已经有了答案: Typescript Function with Generic Return Type (2 个回答) 4 个月前关闭。 以下代码抛出错误: 'T' could be
请参阅以下 34 个字符的 ASCII 字符串。我试图在这个字符串上有 3 个 MD5 哈希匹配。 8AC905DD4AB2038E5F7EABEAE792AC41BC [A-F0-9]{32}仅匹配
假设我有一个充满 javascript 文件的目录: . +-- my_dir +-- apple.js +-- banana.js +-- main.js 子目录中的每个文件都包含一
找不到如何提出关于 Steve Pomeroy 发布的“使用 Android 播放任意音调”的问题,所以开始了一篇 here . 是否有任何代码需要添加到 xml 文件中? 无法让 sim 发出声音。
SO_RCVTIMEO 根本没有在我的输出函数中创建超时条件。我正在使用 ARQ 滑动窗口协议(protocol)设计文件传输服务。为了保持一切有序,我从基本的“停等”开始。 这是我对 timeval
我将在当前工作的网站上创建竞赛。每个比赛都不会相同,并且可能有不同数量的输入字段,用户必须输入这些字段才能成为比赛的一部分,例如。 比赛 1 可能只需要一个名字 比赛 2 可能需要名字、姓氏和电子邮件
我有一个在 Android OpenGL-ES 上渲染的点云。我可以正确翻译它(我认为)但是当我旋转它时,我无法让它按预期工作。我希望它围绕点云的中心旋转(我有这个 3D 点),但我不知道该怎么做。
是否可以为某些 PostgreSQL 函数定义任意返回类型? 这个想法是,根据调用的不同,可能会返回不同的输出。 例如,假设我们有: TypeA : (name, email) TypeB : (na
我是一名优秀的程序员,十分优秀!