js中createElement的使用

来源:互联网 发布:js实现隐藏div 编辑:程序博客网 时间:2024/05/20 09:45

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

//创建表
var _table=document.createElement_x_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_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_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_x('label');
_label.setAttribute('for', _input);
_label.appendChild(document.createTextNode('my check label'));
_td.appendChild(_label)

//创建一个button
_input=document.createElement_x_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中

原创粉丝点击