Javascript node.setAttribute(';attName';,null)返回类型不匹配

Javascript node.setAttribute(';attName';,null)返回类型不匹配,javascript,xml,dom,setattribute,Javascript,Xml,Dom,Setattribute,以下代码返回IE9中的类型不匹配错误 node.setAttribute('attributeName',null); 但是 这也很好: node.setAttribute('attributeName','null'); 你知道为什么会发生这种情况吗?有什么好办法可以解决吗 一个解决办法是检查 if (attributeVal === null){ attributeVal = 'null'; }, node.setAttribute('attributeName

以下代码返回IE9中的类型不匹配错误

 node.setAttribute('attributeName',null);
但是

这也很好:

 node.setAttribute('attributeName','null');
你知道为什么会发生这种情况吗?有什么好办法可以解决吗

一个解决办法是检查

 if (attributeVal === null){
      attributeVal = 'null';
 },  
 node.setAttribute('attributeName',attributeVal);

有什么建议吗?

如果要删除属性,请不要使用
setAttribute(name,null)
,而是使用
removeAttribute(name)

我不确定为什么要设置falsy值,我假设您要么要设置某个值,要么删除该属性。比如:

attributeVal ? 
    node.setAttribute(attributeName, attributeVal) : 
    node.removeAttribute(attributeName);

某些类型的节点不接受属性。在设置“attributename”之前,请检查类型。请注意,要设置的第二个参数应该是字符串,而不是伪对象,这可能就是类型不匹配的原因。这很有意义。问题是服务器需要一个特定的属性,它是在遵循此协议的其他客户机上实现的。
attributeVal ? 
    node.setAttribute(attributeName, attributeVal) : 
    node.removeAttribute(attributeName);