当前位置: 首页 > 图文教程 > 网络编程 > Javascript > tbody元素支持嵌套的注意方法

Javascript
清华大学出版的事半功倍系列 javascript全部源代码
一个不错的渐隐文字效果
JavaScript多线程的实现方法
广告代码静态化js通用函数
“增强js程序代码的健壮性”之我见大量示例代码
国外JavaScript经典封装集合整理
Highslide JS
一个漂亮的下拉列表,去掉了灰色倒三角按钮
Add a Picture to a Microsoft Word Document
Javascript注入技巧
用JS实现HTML标签替换效果
发布BlueShow v1.0 图片浏览器(类似lightbox)blueshow.js 打包下载
模拟弹出菜单的代码
收藏Javascript中常用的55个经典技巧
Track Image Loading效果代码分析
js小技巧--自动隐藏红叉叉
JavaScript延时效果比较不错的
非常酷的js图形渐隐导航菜单栏
科讯商业版中用到的ajax空间与分页函数
js实现的修改div里img标签的src属性

Javascript 中的 tbody元素支持嵌套的注意方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 363 ::
收藏到网摘: n/a

function addMessage(messageID,userName,userCreateDate,articleCount,subject,body,creationDate,modifiedDate)
{
var br;
var row = document.createElement("tr");
var cell = document.createElement("td");
var cellTr = document.createElement("tr");
var cellTd = 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即
<script defer>
</script>

4>另外尽量采取对传值对象的封装,调用一次即可。即,可以将你的表格数据封装成[]或{}(当然内部有模型实现)的策略。
5>如果你js好的话,可以采用prototype做得更完美!
以上意见仅供参考。