NEW()函數(shù)中table=document.getElementById("table").lastChild;為什么不是table=document.getElementsByTagName("table");?
<!DOCTYPE html>
<html>
?<head>
? <title> new document </title> ?
? <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> ??
? <script type="text/javascript">?
??
? ? ? window.onload = function(){
? ? ? ? ? ? var tr=document.getElementsByTagName("tr");
? ? ? ? ? ? for(var i=0;i<tr.length;i++){
? ? ? ? ? ? ? ?acChange(tr[i]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ?function acChange(obj){
?obj.onmouseover=function(){
? ? ? ? ? ? ? ? ? ? obj.style.backgroundColor="#f2f2f2";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? obj.onmouseout=function(){
? ? ? ? ? ? ? ? ? ? obj.style.backgroundColor="#fff";}
}
? ? ? // 編寫一個(gè)函數(shù),供添加按鈕調(diào)用,動(dòng)態(tài)在表格的最后一行添加子節(jié)點(diǎn);
?function NEW(){
? ? ? ? var td1=prompt('請(qǐng)輸入學(xué)號(hào)','');
? ? ? ? var td2=prompt('請(qǐng)輸入姓名','');
? ? ? ? var newtr=document.createElement('tr');
? ? ? ? newtr.innerHTML='<td>'+td1+'</td>'+'<td>'+td2+'</td>'+'<td><a href="javascript:;" onclick="REMOVE(this)">刪除</a></td>';
? ? ? ? var table=document.getElementById("table").lastChild;
? ? ? ? ? ? table.appendChild(newtr);
var tr=document.getElementsByTagName("tr");
? ? ? ? ? ? for(var i=0;i<tr.length;i++){
? ? ? ? ? ? ? ?acChange(tr[i]);
? ? ? ? ? ? ? ? }
? ? ? ??
? ? ?}
? ?
? ? ?
? ? ?// 創(chuàng)建刪除函數(shù)
? ? ?function REMOVE(obj){
? ? ? ? var parent=obj.parentNode.parentNode;?
? ? ? ? var Parent=parent.parentNode;
? ? ? ? Parent.removeChild(parent);
? ? ?}
? </script>?
?</head>?
?<body>?
? <table border="1" width="50%" id="table">
? <tr>
<th>學(xué)號(hào)</th>
<th>姓名</th>
<th>操作</th>
? </tr> ?
? <tr>
<td>xh001</td>
<td>王小明</td>
<td><a href="javascript:;"onclick="REMOVE(this)" >刪除</a></td> ? <!--在刪除按鈕上添加點(diǎn)擊事件 ?-->
? </tr>
? <tr>
<td>xh002</td>
<td>劉小芳</td>
<td><a href="javascript:;"onclick="REMOVE(this)" >刪除</a></td> ? <!--在刪除按鈕上添加點(diǎn)擊事件 ?-->
? </tr> ?
? </table>
? <input type="button" value="添加一行" onclick="NEW()" /> ? <!--在添加按鈕上添加點(diǎn)擊事件 ?-->
?</body>
</html>
2016-10-09
返回值不一樣,后者是數(shù)組
2016-10-09
id不是table嗎 ?也可以用getElementsByTagName("table")[0];
2016-10-09
添加到節(jié)點(diǎn)的最后一行