我试图使用JavaScript从一个DOM节点中删除属性:
<div id="foo">Hi there</div>
首先,我添加一个属性:
document.getElementById("foo").attributes['contoso'] = "Hello, world!";
然后我将其移除:
document.getElementById("foo").removeAttribute("contoso");
除非属性仍然存在。
所以我尝试真正地将其移除:
document.getElementById("foo").attributes['contoso'] = null;
现在它的值是null
,与它开始时不同,那时候它是undefined
。
从元素中删除属性的正确方法是什么?
注意:用属性required
替换属性contoso
,你就会明白我试图做什么了。
状态表
foo.attributes.contoso foo.hasAttribute("contoso")
====================== ===========================
Before setting undefined false
After setting Hello, world! false
After removing Hello, world! false
After really removing null false
property
和从DOM对象中删除一个attribute
。如果这是一个Javascript对象,它就可以运行。 - Ian Boyd