HTML - 为什么不是不确定=“不确定|真”受尊重?

发布于 2025-01-07 05:12:59 字数 328 浏览 0 评论 0原文

我想使用复选框的不确定属性。更改 JavaScript 中的值适用于各种浏览器(甚至 MSIE6!),但是,我无法通过 any 中的 HTML 属性设置初始值。

这是设计使然吗? 如果是这样,为什么?在服务器端,我可以确定它是不确定的。那么,为什么我不能告诉浏览器呢?如果长时间运行的脚本保留了属性设置,浏览器供应商难道不担心 FOUC(无样式内容的闪烁)吗?

这是一个工作示例: http://jsfiddle.net/KUQC9/1/

I want to use the indeterminate property of a checkbox. Changing the value in JavaScript works in all sorts of browsers (even MSIE6!), however, I cannot set the initial value via HTML attributes in any.

Is this by design? If so, why? On server side, I can determine that it's indeterminate. So, why can't I tell the browser? Weren't browser vendors worried about FOUCs (Flashes Of Unstyled Content) if a long-running script holds up the property-setting?

Here's a working example: http://jsfiddle.net/KUQC9/1/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自我难过 2025-01-14 05:12:59

您无法通过 HTML 设计使复选框变得不确定。请参阅 css-tricks.com 上的这篇文章,其中深入讨论了该主题。

至于你的问题的“为什么”部分,W3C 邮件列表上有一些信息可能会有所帮助:

  • 看起来好像考虑了为复选框提供三态值(看似已选中、未选中、不确定)的想法,但由于向后兼容性问题而未实现
  • 唯一提及真正的不确定 HTML正如您在示例中所示,我可以在这里找到“内容”属性:

。这是值得的,实际上有一个 :infiniteate CSS 选择器(它似乎像其他 CSS 伪类一样对待,如 :visited ,它也不能直接通过 HTML 设置,所以也许这也是 indefinate 不能成为的另一个原因)。在 CSS 选择器的演示中,W3C 本身使用了与您大致相同的方法:W3C 不确定 CSS 选择器演示。鉴于此,我想说您正在使用可用于设置复选框的不确定状态的最佳方法

You can't make a checkbox indeterminate through HTML by design. See this article on css-tricks.com, which discusses the topic in depth.

As for the "why" part of your question, there is some information on the W3C mailing lists that may be helpful:

  • It looks as if the idea of giving checkboxes a tri-state value (seemingly checked, unchecked, indeterminate) was considered, but not implemented because of backwards-compatibility issues
  • The only mention of a true indeterminate HTML "content" attribute, as you show in your example, that I could find was here:

For what it's worth, there is in fact an :indeterminate CSS selector (it seems to be treated like other CSS pseudo-classes like :visited, which also cannot be set through HTML directly, so maybe that is another reason indeterminate can't be either). In its demo of the CSS selector the W3C itself uses about the same approach that you do: W3C indeterminate CSS selector demo. In light of that, I'd say you're using about the best method available for setting the indeterminate state of a check box

昵称有卵用 2025-01-14 05:12:59

这是设计使然。表示复选框状态的唯一 HTML 方式是通过 selected 属性;这是因为复选框意味着真/假,而不是真/假/未知。可以通过 JavaScript 来标记不确定的内容,但它不会更改复选框的实际/提交值。

如果您确实想将复选框初始化为不确定,则需要使用 Javascript 来完成(我想是在加载事件上)。

It is by design. The only HTML way to denote checkbox state is through the checked attribute; this is because checkbox is meant to true/false, rather than true/false/unknown. Marking something indeterminate can be done via Javascript, but it will not change the actual/sumbitted value of the checkbox.

If you really want to initialize checkboxes to indeterminate, you'll need to do it with Javascript (I would imagine on the load event).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文