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

Javascript
符合标准的对联广告
Firefox 无法获取cssRules 的解决办法
简单JS代码压缩器
不错的asp中显示新闻的功能
动态加载js文件 document.createElement
[原创]静态页面也可以实现预览 列表不同的显示方式
JS代码格式化和语法着色V2
关于javascript的“静态类"
显示/隐藏侧边栏
网页取色
cancelBubble阻止事件冒泡
屏蔽alt+f4代码,一个变通的方法
光标的一些操作总结
使用TextRange获取输入框中光标的位
Using the TextRange Object
textbox右键菜单
document.createRange实例
模仿IE自动完成功能
计算100000数组js脚本的执行时间
怎么让脚本或里面的函数在所有图片都载入完毕的时候执行

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 256 ::
收藏到网摘: 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做得更完美!
以上意见仅供参考。