HTML中有一些属性是“布尔” - 如果存在,浏览器会将它们视为“真”,无论其值如何。在<option>
标签上选择了这种属性的一个例子。另一个是在<input type="checkbox">
上查看的。
如果您对setAttribute()
调用了这样的属性,那么似乎没有任何值可以设置为使浏览器始终如一,就好像缺少属性一样。
例如
option.setAttribute("selected", false)
仍将标记所选的选项。 null,空字符串或undefined也不起作用。如果有人知道我可以设定的价值来实现我的目标,请告诉我,但我认为不存在。 (由于我使用的一些框架代码,不调用setAttribute()
,或调用removeAttribute()
很困难。)
我试图在特殊情况下找到这些属性的详尽列表。这是我到目前为止所拥有的:
<option>
<input>
<input>
,<select>
,<option>
,<optgroup>
,<button>
,<textarea>
<select>
的倍数请帮我填写此列表 - 或者指向现有的列表。
(由于我使用的一些框架代码,不调用setAttribute(),或调用removeAttribute()很困难。)
然后我会提交框架代码需要修复或转储。
按设计,您无法通过setAttribute取消设置属性。如果碰巧在任何特定浏览器中工作,那么您发现任何涉及带外值的解决方案(如“null”)将根据DOM Core标准完全无效。
无论如何,在浏览器(非XML)HTML上下文中最好避免使用setAttribute()。 IE pre-8不知道DOM属性和JavaScript属性之间的区别,这很容易导致许多非常奇怪的问题。如果你试图将'checked'设置为一个属性(理论上你应该把它设置为字符串“checked”),不要指望IE合作。
HTML 4.01(以及XHTML 1.0)中的布尔属性的完整列表是(具有不同的属性名称):
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)
option.removeAttribute("selected");
编辑:阅读你的评论后,阅读关于setAttribute的this。值得注意的是:
即使getAttribute()为缺少的属性返回null,也应该使用removeAttribute()而不是elt.setAttribute(attr,null)来删除属性。
在表格上例如TD,TH
nowrap
对于记录,可以更改像check(在复选框/无线电元素上)的属性。
myCheckBoxElem.checked = true|false;
要么
myCheckBoxElem.checked = !myCheckBoxElem.checked;//toggles to the opposite state
你能不能只使用removeAttribute()
?
不完全是你所要求的,但'class'和'for'属性都接收不同的DOM名称
element.className
element.htmlFor