星期二, 3月 04, 2008

JavaScript動態產生html tag

//table
var tr = document.createElement("tr");
var td = document.createElement("td");
td.innerText = "this is the text of td";
tr.appendChild(td);


//button
butt = document.createElement('BUTTON');
butt.appendChild(document.createTextNode('click me!'));
butt.onclick = downloadCourse;


//a
var aElement = document.createElement("< a href=javascript:go() >< /a>");
a的href可以直接呼叫javascript的function


//input
var input1 = document.createElement("input"); // create input node
input.type = "checkbox"; // set type, others like text,radio
input.name ="chk"; // set name as its id

*要在input後面加字,即利用appendChild(document.createTextNode('xx'))

*快速產生
var input2 = document.createElement("< input type=button>"); 


//add attribute
var div = document.getElementById('div1'); 
var attr = document.createAttribute('class'); 
attr.value = 'cssClass1'; 
div .setAttributeNode(attr);

沒有留言: