布尔 HTML 属性 [英] Boolean HTML Attributes

查看:28
本文介绍了布尔 HTML 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML 中有一些属性是布尔值"——如果它们存在,浏览器将它们视为真",而不管值如何. 标签上的 selected 就是此类属性的一个示例.另一个在 检查.

There are some attributes in HTML which are "boolean" - browsers treat them as "true" if they are present, regardless of the value. An example of such an attribute is selected on the <option> tag. Another is checked on <input type="checkbox">.

如果您为这样的属性调用了 setAttribute(),似乎没有您可以设置的值来让浏览器始终表现得好像缺少该属性一样.

If you have a call to setAttribute() for such an attribute, there seems to be no value you can set to have the browsers consistently behave as though the attribute is missing.

例如

option.setAttribute("selected", false)

仍会标记选中的选项.null、空字符串或 undefined 也不起作用.如果有人知道我可以设定的价值来实现我的目标,请告诉我,但我认为不存在.(因为我使用了一些框架代码,调用setAttribute(),或者调用removeAttribute()很困难.)

will still mark the option selected. null, empty string or undefined don't work either. If anyone knows of a value I can set to achieve my goal, please let me know, but I don't think one exists. (Because of some framework code I use, not calling setAttribute(), or calling removeAttribute() is difficult.)

我试图找到这些属性的详尽列表,以对它们进行特殊处理.这是我到目前为止所拥有的:

I'm trying to find an exhaustive list of such attributes to special case them. Here's what I have so far:

  • selected of
  • 检查
  • 禁用只读