まず第一にsetattribute
、大文字と小文字を区別するため、要素の未定義のプロパティですが、setAttribute
もちろん使用可能です。
第二に、絶対に使用する必要はありませんsetAttribute
。プロパティを変更するだけid
で、まったく同じ効果が得られます。
var el = document.createElement('div'), // your element
staticPart = 'myUniqId', // your static part of a unique id
i = 0; // your uniqueness
el.id = staticPart + i; // assign unique id to the element
// el.setAttribute('id', staticPart + i); // does the same, but more verbose
// Let's check if it worked:
el.getAttribute('id'); // "myUniqId0"
el.id; // "myUniqId0"
第三に、どこでそれを見ましたか
setattributeの値はリテラルのみにすることができます
仕様によると、はDOMstring型のsetAttribute
2つのパラメーターを受け入れname
ます。任意の値を渡すことができます。文字列に変換されることに注意してください。value
el.id = {a: 'b'};
el.id; // "[object Object]"
http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-F68F082およびhttps://developer.mozilla.org/en-US/docs/DOM/element.setAttributeを参照してください