js中createElement的使用

//創建form
var _form=document.createElement_x('form');
_form.setAttribute('name','myform');
_form.setAttribute('action','');
_form.setAttribute('method','post');

//創建表
var _table=document.createElement_x('table');
_table.setAttribute('border', '1');
_table.setAttribute('borderColor', 'red');
_table.setAttribute('width', '300');
_table.setAttribute('height', '100');

//創建一行
var _tr=_table.insertRow(_table.rows.length);
// _tr.rowIndex //當前行的行號

//創建一列
var _td=_tr.insertCell(_tr.cells.length);

//給<td>添加文本
_txt=document.createTextNode('Intitul');
_td.appendChild(_txt);
alert(_td.contentEditable=true);

//創建一個checkbox
var _input=document.createElement_x('input');
_input.setAttribute('type', 'checkbox');
_input.setAttribute('name', 'mycheck');
_input.setAttribute('value', 'ddddd');
_td.appendChild(_input);
_input.defaultChecked=true;

//創建一個radio
var _input=document.createElement_x('input');
_input.setAttribute('type', 'radio');
_input.setAttribute('name', 'myradio');
_input.setAttribute('value', 'ddddd');
_input.defaultChecked=true;
_td.appendChild(_input);

//給checkbox添加
var _label=document.createElement_x('label');
_label.setAttribute('for', _input);
_label.appendChild(document.createTextNode('my check label'));
_td.appendChild(_label)

//創建一個button
_input=document.createElement_x('button');
_input.setAttribute('type', 'submit');
_input.setAttribute('name', 'mysubmit');
_input.setAttribute('value', 'my submit');
_input.setAttribute('size', '130');
_td.appendChild(_input);

//把表格附加到父容器內
_form.appendChild(_table);
document.body.appendChild(_form);
</script>

 

注意:tr不能appendChild到table中

發佈了32 篇原創文章 · 獲贊 3 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章