解決ie6 ie7中js不能通過appendChild("tr")添加table行的方法

 

本機只有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,這樣代碼才能使用更多瀏覽器。看下面一段代碼:

Html代碼 複製代碼 收藏代碼
  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <script type="text/javascript">  
  5.         function insRow()   
  6.         {   
  7.             var tbl = document.getElementById('myTable');   
  8.             var row = tbl.insertRow(0);   
  9.             var cell = row.insertCell(0);   
  10.             cell.innerHTML="new cell";   
  11.         }   
  12. </script>  
  13.     </head>  
  14.   
  15.     <body>  
  16.         <table id="myTable" border="1">  
  17.             <tr>  
  18.                 <td>  
  19.                     cell   
  20.                 </td>  
  21.             </tr>  
  22.         </table>  
  23.         <br />  
  24.         <input type="button" onclick="insRow()" value="Insert row">  
  25.     </body>  
  26. </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);

 

 

下面附加一段帶有 在最後加一行,本行前前加一行,本行後加一行,刪除當前行的html代碼

Html代碼 複製代碼 收藏代碼
  1. <!DOCTYPE html>  
  2. <html>  
  3.     <head>  
  4.         <meta http-equiv="Content-Type" content="text/htmt;charset=utf-8">  
  5.         <script type="text/javascript">  
  6.         var i=0;   
  7.         function insRow(){   
  8.             var tbl = document.getElementById("myTable");   
  9.             insRowIndex(tbl.rows.length);   
  10.         }   
  11.         function insRowIndex(rowIndex){   
  12.             var tbl = document.getElementById("myTable");   
  13.             var row = tbl.insertRow(rowIndex);   
  14.             var cell0 = row.insertCell(0);   
  15.             var cell1 = row.insertCell(1);   
  16.             cell0.innerHTML = "cell " + i++;   
  17.             cell1.innerHTML = " <input type='button' value='delete' onclick='delRow(this)'>"  
  18.                                 +"<input type='button' value='insBefore' onclick='insBefore(this)'>"   
  19.                                 +"<input type='button' value='insAfter' onclick='insAfter(this)'>";   
  20.         }   
  21.         function delRow(row){   
  22.             var tbl = document.getElementById("myTable");   
  23.             var rowrowIndex =  row.parentNode.parentNode.rowIndex;   
  24.             tbl.deleteRow(rowIndex);   
  25.         }   
  26.         function insBefore(row){   
  27.             var rowrowIndex =  row.parentNode.parentNode.rowIndex;   
  28.             insRowIndex(rowIndex)   
  29.         }   
  30.         function insAfter(row){   
  31.             var rowrowIndex =  row.parentNode.parentNode.rowIndex;   
  32.             insRowIndex(rowIndex+1)   
  33.         }   
  34. </script>  
  35.     </head>  
  36.   
  37.     <body>  
  38.         <table id="myTable" border="1">  
  39.             <tr>  
  40.                 <td>  
  41.                     單元格   
  42.                 </td>  
  43.                 <td>  
  44.                     操作   
  45.                 </td>  
  46.             </tr>  
  47.         </table>  
  48.         <br />  
  49.         <input type="button" onclick="insRow()" value="Insert row">  
  50.     </body>  
  51. </html> 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章