3

次のコードは、IE9 で型の不一致エラーを返します。

 node.setAttribute('attributeName',null);

しかし

 node.setAttribute('attributeName',undefined) doesn't give an error.

そして、これも問題ありません:

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

なぜこれが起こっているのか、それを修正する良い方法は何ですか。

1つの解決策は、確認することです。

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

助言がありますか ?

4

1 に答える 1

0

属性を削除しようとしている場合は、 を使用せずsetAttribute(name, null)に を使用してくださいremoveAttribute(name)

偽の値を設定する理由がまったくわかりません。何かを設定するか、属性を削除する必要があると思います。次のようなものです:

attributeVal ? 
    node.setAttribute(attributeName, attributeVal) : 
    node.removeAttribute(attributeName);
于 2012-12-13T22:38:57.633 に答える