tbody元素支持嵌套的注意方法
tbody元素支持嵌套的注意方法
发布时间:2016-12-30 来源:查字典编辑
摘要:functionaddMessage(messageID,userName,userCreateDate,articleCount,subj...

functionaddMessage(messageID,userName,userCreateDate,articleCount,subject,body,creationDate,modifiedDate)

{

varbr;

varrow=document.createElement("tr");

varcell=document.createElement("td");

varcellTr=document.createElement("tr");

varcellTd=document.createElement("td");

cellTd.appendChild(document.createTextNode("用户名:"+userName));

cellTr.appendChild(cellTd);

cell.appendChild(cellTr);

cellTd=document.createElement("td");

cellTd.appendChild(document.createTextNode("创建时间:+userCreateDate"));

cellTr.appendChild(cellTd);

cell.appendChild(cellTr);

cellTd=document.createElement("td");

cellTd.appendChild(document.createTextNode("发表文章:"+articleCount));

cellTr.appendChild(cellTd);

cell.appendChild(cellTr);

row.appendChild(cell);

cell=document.createElement("td");

cellTr=document.createElement("tr");

cellTd=document.createElement("td");

cellTd.appendChild(document.createTextNode("发表时间:"+creationDate+""+"修改时间:"+modifiedDate));

cellTr.appendChild(cellTd);

cell.appendChild(cellTr);

cellTr=document.createElement("tr");

cellTd=document.createElement("td");

cellTd.appendChild(document.createTextNode(subject));

br=document.createElement("br");

cellTd.appendChild(br);

cellTd.appendChild(document.createTextNode(body));

cellTr.appendChild(cellTd);

cell.appendChild(cellTr);

row.appendChild(cell);

document.getElementById("messageList").appendChild(row);

}

以上代码在ie中出现"意外的调用了方法或属性访问",错误指向最后一句.望各大侠指点迷津

评价:

你这段代码感觉不优雅~没有重用性~性能底~、

建议:

1>采用函数封装实现重用。

2>对于table的内部嵌套采用如下格式:

<table>

<tbody>

<tr>

<td></td>

...

</tr>

...

</tbody>

<tbody>

</tbody>

...

</table>

对table动态生成,采用从内到外添加的方案。另尽可能少使用document.createTextNode,性能低。

3>如果你的table不是在页面加载时需要执行,建议在<script中添加defer即

<scriptdefer>

</script>

4>另外尽量采取对传值对象的封装,调用一次即可。即,可以将你的表格数据封装成[]或{}(当然内部有模型实现)的策略。

5>如果你js好的话,可以采用prototype做得更完美!

以上意见仅供参考。

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新Javascript教程学习
热门Javascript教程学习
编程开发子分类