晕了,Javascript动态添加表不起作用
首先页面中有个panel1的div,然后Javascript创建个table,并设置样式,最后添加到div总,代码是这样的:
<script type= "text/javascript ">
this._displayDiv = document.getElementById( "panel1 ");
//Create DOM Element
this._viewTable = document.createElement( "table ");
this._viewTr = document.createElement( "tr ");
this._viewTd1 = document.createElement( "td ");
this._viewTd2 = document.createElement( "td ");
//Set Table Style
this._viewTable.width = "100% ";
this._viewTable.height = "100% ";
this._viewTable.border= "0 px ";
this._viewTable.cellpadding = "0 px ";
this._viewTable.cellspacing = "0 px ";
//Set td1 Style
this._viewTd1.width = "20 px ";
this._viewTd1.innerHTML = "test ";
//Set td2 Style
this._viewTd2.style.paddingLeft = "8 px ";
this._viewTd2.style.fontSize = "12 px ";
this._viewTd2.style.fontFamily = "Arial ";
this._viewTd2.innserHTML = this._defaultText;
//Join Elements
this._displayDiv.appendChild(this._viewTable);
this._viewTable.appendChild(this._viewTr);
this._viewTr.appendChild(this._viewTd1);
this._viewTr.appendChild(this._viewTd2);
</script>
可是最后并没加进去。。。
------解决方案--------------------使用
table.insertRow(),row.insertCell()代替
document.createElement( "tr ");document.createElement( "td ");
之后不需要appendChild()
如此即可.
------解决方案--------------------在IE下,tr的parentElement是tbody,而不是table,所以你加不上去
再创建个tbody看看吧
------解决方案--------------------用insertRow和insertCell确实比较方便
//tr1
tempRow = this._viewTable.insertRow();