布尔 HTML 属性 [英] Boolean HTML Attributes
问题描述
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
- 检查的
- 禁用、
的只读、
、
<选项>
、、
、
- 多个
- selected of
<option>
- checked of
<input>
- disabled, readonly of
<input>
,<select>
,<option>
,<optgroup>
,<button>
,<textarea>
- multiple of
<select>
请帮助我完成此列表 - 或向我指出现有列表.
Please help me complete this list - or point me to an existing one.
推荐答案
(因为我使用了一些框架代码,不调用 setAttribute() 或调用 removeAttribute() 很困难.)
(Because of some framework code I use, not calling setAttribute(), or calling removeAttribute() is difficult.)
然后我会提交框架代码需要修复或转储.
Then I would submit that the framework code needs fixing, or dumping.
根据设计,您无法通过 setAttribute 取消设置属性.根据 DOM Core 标准,您发现的任何涉及带外值(如null")的解决方案(如果它碰巧在任何特定浏览器中工作)都将是无效的.
You can't setAttribute to unset an attribute, by design. Any solution you found involving out-of-band values like ‘null’, if it happened to work in any particular browser, would be quite invalid according to the DOM Core standard.
setAttribute() 无论如何最好避免在浏览器(非 XML)HTML 上下文中使用.IE pre-8 不知道 DOM 属性和 JavaScript 属性之间的区别,这很容易导致许多非常奇怪的问题.如果您尝试将checked"设置为一个属性(理论上您应该通过将其设置为字符串checked"来实现),不要指望 IE 能够合作.
setAttribute() is in any case best avoided in browser (non-XML) HTML contexts. IE pre-8 doesn't know the difference between a DOM attribute and a JavaScript property, which can easily result in many really weird problems. If you're trying to set ‘checked’ as an attribute (which theoretically you should do by setting it to the string "checked"), don't expect IE to co-operate.
HTML 4.01(以及 XHTML 1.0)中布尔属性的完整列表是(带有不同大小写的属性名称):
The full list of boolean attributes in HTML 4.01 (and hence XHTML 1.0) is (with property names where they differ in case):
checked (input type=checkbox/radio)
selected (option)
disabled (input, textarea, button, select, option, optgroup)
readonly (input type=text/password, textarea)
multiple (select,input)
ismap isMap (img, input type=image)
defer (script)
declare (object; never used)
noresize noResize (frame)
nowrap noWrap (td, th; deprecated)
noshade noShade (hr; deprecated)
compact (ul, ol, dl, menu, dir; deprecated)
这篇关于布尔 HTML 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!