解决ie6 ie7中js不能通过appendChild("tr")添加table行的方法
来源:互联网 发布:手机淘宝店招尺寸2017 编辑:程序博客网 时间:2024/06/02 20:33
本文转自:http://blog.csdn.net/xiaoning8201/article/details/6884201
本机只有ie7,ff3.5,opera10,测试在这三个环境中测试通过。
在标准DOM中添加元素一般使用appendChild();
但用js在table中添加行时却失效了。
网上搜了一下说 ie6,ie7不支持table.appendChild("tr")
那在JavaScript中怎么在一个table中添加一行呢?
在http://www.w3schools.com/htmldom/dom_obj_table.asp看到w3c文档中HTML DOM Object存在tableObject.insertRow(index)方法。何不在插入行时用这个方法呢,毕竟在html中table比普通的标签有其特殊性,碰到table添加一行时,注意使用insertRow而不是appendChild,这样代码才能使用更多浏览器。看下面一段代码:
<!DOCTYPE html> <html> <head> <script type="text/javascript"> function insRow() { var tbl = document.getElementById('myTable'); var row = tbl.insertRow(0); var cell = row.insertCell(0); cell.innerHTML="new cell"; } </script> </head> <body> <table id="myTable" border="1"> <tr> <td> cell </td> </tr> </table> <br /> <input type="button" onclick="insRow()" value="Insert row"> </body> </html>
<!DOCTYPE html> <html> <head> <script type="text/javascript"> function insRow() { var tbl = document.getElementById('myTable'); var row = tbl.insertRow(0); var cell = row.insertCell(0); cell.innerHTML="new cell"; } </script> </head> <body> <table id="myTable" border="1"> <tr> <td> cell </td> </tr> </table> <br /> <input type="button" onclick="insRow()" value="Insert row"> </body> </html>
比使用标准的DOM还简单,而且也符合w3c标准,但有一点要说明的是:
innerHTML这个方法虽然没有在w3c文档中出现,但是由于使用的广泛性,很多浏览器都进行了支持,添加文本节点(text nodes)时可以用innerHTML,如果非要符合w3c标准,可以用createTextNode(str)方法,本例中在JavaScript最后一行改为:cell.appendChild(document.createTextNode("new cell"))。
但是上面的例子还有一个与appendChild()不同的地方,就是appendChild值插在原有元素的后面,但是例子中是插在了第一行。怎么插在表格的最后一行,或者插在当前行的后一行或者前一行怎么做呢?
只要实例中把javascript改为:var row = tbl.insertRow(tbl.rows.length);
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/htmt;charset=utf-8"> <script type="text/javascript"> var i=0; function insRow(){ var tbl = document.getElementById("myTable"); insRowIndex(tbl.rows.length); } function insRowIndex(rowIndex){ var tbl = document.getElementById("myTable"); var row = tbl.insertRow(rowIndex); var cell0 = row.insertCell(0); var cell1 = row.insertCell(1); cell0.innerHTML = "cell " + i++; cell1.innerHTML = " <input type='button' value='delete' onclick='delRow(this)'>" +"<input type='button' value='insBefore' onclick='insBefore(this)'>" +"<input type='button' value='insAfter' onclick='insAfter(this)'>"; } function delRow(row){ var tbl = document.getElementById("myTable"); var rowrowIndex = row.parentNode.parentNode.rowIndex; tbl.deleteRow(rowIndex); } function insBefore(row){ var rowrowIndex = row.parentNode.parentNode.rowIndex; insRowIndex(rowIndex) } function insAfter(row){ var rowrowIndex = row.parentNode.parentNode.rowIndex; insRowIndex(rowIndex+1) } </script> </head> <body> <table id="myTable" border="1"> <tr> <td> 单元格 </td> <td> 操作 </td> </tr> </table> <br /> <input type="button" onclick="insRow()" value="Insert row"> </body> </html>
- 解决ie6 ie7中js不能通过appendChild("tr")添加table行的方法
- 解决ie6 ie7中js不能通过appendChild("tr")添加table行的方法
- 解决ie6 ie7中js不能通过appendChild("tr")添加table行的方法
- FAQ_IE 8中还不支持JavaScript通过appendChild("tr')方法添加Table行
- js中document.body.appendChild在IE6下不兼容的替换方法
- js中appendChild()方法
- js动态给table添加行(tr)
- js控制table tr 的方法
- Table中删除Tr的JS实现
- 解决IE6,IE7不能隐藏绝对定位溢出的内容
- IE中table标签不支持javascript的appendChild方法
- IE6 IE7: div中 table 宽度100% 引发的血案
- js实现table添加行tr、删除行tr、清空行tr
- JS技巧:JS区别IE6、IE7、IE8之间的方法
- js向table表格中添加tr行代码(分页有待改善)
- 转js动态给table添加行(tr)
- 利用AJAX生成自动添加tr的table,并且为添加的tr附加上方法
- js动态创建table节点并通过appendChild拼接子元素的问题
- android 橘子汁 在子线程中更新主线程UI 困扰
- ios添加pre和post build action
- 如何让应用程序获得系统权限以及如何使用platform密钥给apk签名
- 删除用户
- PowerDesigner生成sql及说明文档
- 解决ie6 ie7中js不能通过appendChild("tr")添加table行的方法
- PostgreSQL的备份与还原
- Linux_problem 1: ntfs-3g
- 数据库模式
- IE8下,js调用另一页面js方法,不能alert问题
- java基础——异常处理
- 【deep learning学习笔记】注释yusugomori的LR代码 --- 模型测试
- 修改和删除概要文件
- jQueryMobile学习笔记二