利用js事件进行简单的表格操作

来源:互联网 发布:日本漫画哪个软件最好 编辑:程序博客网 时间:2024/06/05 03:45

<table id="table1">
 <tr>
  <td>1</td><td>aaa</td><td>aaa</td><td><input type="button" value="save" /></td>
 </tr>
 <tr>
  <td>2</td><td><input type="text" value="bbb" /></td><td>aaa</td><td><input type="button" value="save" onClick="showContent();" /></td>
 </tr>
 <tr>
  <td>3</td><td>aaa</td><td>aaa</td><td><input type="button" value="save" /></td>
 </tr>
</table>
<script language="javascript">

document.onclick = function(e){
e = window.event || e;
e = e.srcElement || e.target;
if( e.tagName && e.tagName.toLowerCase() == "td" )
alert("行号:" + e.parentNode.rowIndex + "/n" + "列号:" + e.cellIndex);
}
s=document.getElementById("table1").rows[1].cells[1].getElementsByTagName("input")[0].value;
alert(s);

function showContent(){
 e = window.event || e;
 e = e.srcElement || e.target;
 e = e.parentNode;
 var content = document.getElementById("table1").rows[e.parentNode.rowIndex].cells[1].getElementsByTagName("input")[0].value;
 alert(content);
}
</script>

这个方法只能用于ie系列浏览器。