Sunspl

Hello,everyone,i am sun. 天道酬勤,笨鳥先飛.
随笔 - 47, 文章 - 0, 评论 - 24, 引用 - 0
数据加载中……

在JSP頁面用JS增加TABLE行及刪除行

<button onClick="add()">add</button>
<button onClick="del()">del</button>
<button onClick="test()">test</button>
<table id="t1" border="1"></table>
<script language="JavaScript">
var n=0;
function add(){
n++;
t1.insertRow().insertCell().innerHTML = '<input type="radio" name="tt"><input name="test'+n+'"><button onclick="deleteRow(this)">delthis</button>';
//t1.insertRow().insertCell().innerHTML = '<input type="radio" name="tt"><input name="test'+t1.rows.length+'">';
}
function del(){
 var c = document.getElementsByName('tt');
 for(var i=0; i<c.length; i++)
 if(c[i].checked)
 t1.deleteRow(i);
}
function deleteRow(obj){
 alert(obj.parentElement.parentElement.parentElement.parentElement.id);
 alert(obj.parentElement.parentElement.rowIndex);
//t1.deleteRow(obj.parentElement.parentElement.rowIndex);
}
function test(){
for(i=0;i<t1.rows.length;i++) alert(t1.rows[i].cells[0].innerHTML);
}
</SCRIPT>

 

<html>
<body>
<form id="myForm">
<table id="table1">
 <tr id="tr1">
  <td id="td1">
<input id="myButton"
       type="button" value="Show my parent" onclick="function1();">
      </td>
    </tr>
  </table>
</form>
<script language="JavaScript">
    function function1() {
        var m = document.all.myButton.parentElement.parentElement.parentElement.parentElement.id;
        alert("Parent element: <FORM>, ID = "+'"'+m+'"');
    }
</script>
cloneNode()方法克隆表格:
<input type=button value=clone name=ok onclick=add()>
<table border=1>
<tr id=a1 name=a1>
<td><input></td><td><input></td>
</tr>
</table>
<script language=JavaScript>
i=1
function add(){
++i;
var newTR = a1.cloneNode(true);
newTR.id="a"+i;
newTR.name="a"+i;
a1.parentNode.insertAdjacentElement("beforeEnd",newTR);
//alert(a1.parentElement.innerHTML);
}
</script>

appendChild() & removeChild()方法:(firefox only)
<div id="oTest">1234 </div>
<button onclick="oT=document.getElementById('oTest');oS=document.createElement('span');oS.textContent='1234 ';oT.appendChild(oS)">append +</button><button onclick="oT=document.getElementById('oTest');oT.removeChild(oT.lastChild)">remove -</button>

posted on 2007-04-16 16:17 JavaSuns 阅读(3504) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问