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

Javascript
javascript进行客户端数据的校验
javascript中如何实现浏览器上的右键菜单
用javascript使链接按钮不断变化
利用javascript制作倒计时牌
用javascript实现变色背景和文字
让弹出窗口变得“体贴”一些(javascript)
javascript实例教程(1) 创建弹出式窗口
javascript实例教程(2) 创建折叠式导航菜单
javascript实例教程(3) 探测浏览器插件
javascript实例教程(4) 探测浏览器插件
javascript实例教程(5) 在一个表单中设置和检查Cookies
javascript实例教程(6) 利用javascript进行密码保护
javascript实例教程(7) 利用javascript基于浏览器类型的重定向
javascript实例教程(8) 检验表单有效性
javascript实例教程(9) 随机显示图片
javascript实例教程(10) 创建后退按钮
javascript实例教程(11) 隐藏script代码
javascript实例教程(12) 鼠标移过时报警
javascript实例教程(13) 鼠标触发窗口
javascript实例教程(14) JS代替CGI

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


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