gpt4 book ai didi

css - :required or [required] CSS selector

转载 作者:行者123 更新时间:2023-12-05 03:20:18 28 4
gpt4 key购买 nike

浏览 MDN 文档以查看提议的伪类的状态时,我遇到了 :required(并且扩展为 :optional)。这两个都已经存在很长时间了,但我现在才刚刚了解到。

此选择器与使用属性选择器 [required] 有何不同。有区别吗?

input:requiredinput[required]。我承认 input:optionalinput:not([required]) 更好。

如果没有任何区别,应该使用哪个或者根本不重要?为什么在属性选择器出现时引入这两个?

最佳答案

input:required {
border-color: blue;
}

input[required] {
background-color: pink;
}
<input type="text" required>

Does it differ?

看起来它们在功能上并没有什么不同。

If it doesn't differ in any way, which should be used or does it not matter at all?

在功能上,我认为这没有任何区别。然而,

Why were these two introduced when attribute selectors are around?

:valid 和 :invalid 等伪类一起,:required:optional 提供了一个为 form 元素编写 css 的一致方式。我相信这是预期的目的(没有任何来源,只是我的假设)。

正如您还提到的,它确实有助于减少冗长,例如 :not([required])

关于css - :required or [required] CSS selector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73214891/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com