setAttribute(string name, string value):增加一個指定名稱和值的新屬性,或者把一個現有的屬性設定為指定的值。
1、樣式問題
setAttribute("class", value)中class是指改變"class"這個屬性,所以要帶引號。
vName代表對樣式賦值。
例如:
復制代碼 代碼如下:
var input = document.createElement("input");
input.setAttribute("type", "text");
input.setAttribute("name", "q");
input.setAttribute("class",bordercss);
復制代碼 代碼如下:
element.setAttribute("class", value); //for firefox
element.setAttribute("className", value); //for IE
復制代碼 代碼如下:
var bar = document.getElementById("testbt");
bar.setAttribute("onclick", "javascript:alert('This is a test!');");
復制代碼 代碼如下:
document.getElementById("testbt").className = "bordercss";
document.getElementById("testbt").style.cssText = "color: #00f;";
document.getElementById("testbt").style.color = "#00f";
document.getElementById("testbt").onclick= function () { alert("This is a test!"); }
新聞熱點
疑難解答
圖片精選