2013-09-06 7 views

答えて

15

一つの方法を、setAttributeメソッドを使用することです:ここ

var btn=document.createElement("BUTTON"); 
btn.setAttribute("id", "btn_id"); 
btn.setAttribute("class", "btn_class"); 
btn.setAttribute("width", "250px"); 
btn.setAttribute("data-comma-delimited-array", "one,two,three,four"); 
btn.setAttribute("anything-random", document.getElementsByTagName("img").length); 

利点は、あなたが任意の名前に任意の値を割り当てることができるということで。

element.setAttribute(name, value); 

https://developer.mozilla.org/en-US/docs/Web/API/element.setAttribute

3

あなたは、そのプロパティに割り当てることができます:JavaScriptを使用して

var btn=document.createElement("BUTTON"); 
btn.id = 'btn_id'; 
btn.className = 'btn_class'; 
関連する問題