| 以下代码实现的功能是:动态添加表格行及单元格,同时动态设置行的事件;当鼠标双击表格动态产生的行的时候,传递参数执行函数。
<title>大众计算机学习网</title> <script language="javascript"> //IE function RowClick(RowNum) { alert("您点击的是第"+RowNum+"行"); } var rowIndex=0 function insertRowForIE() { var targetTable = document.getElementById("MyTable"); var objRow=targetTable.insertRow(); rowIndex=rowIndex+1; objRow.setAttribute("id",rowIndex); objRow.setAttribute("ondblclick", RowClick(rowIndex)); var objCell0 = objRow.insertCell(); objCell0.innerHTML="添加的第"+rowIndex+"第1列"; objCell0.setAttribute("width","30%"); objCell0.setAttribute("align", "center") ; var objCell1 = objRow.insertCell(); objCell1.setAttribute("align", "center") ; objCell1.setAttribute("width","30%"); objCell1.innerHTML="添加的第"+rowIndex+"第2列"; var objCell2 = objRow.insertCell(); objCell2.innerHTML="<textarea id='newdesc' name='newdesc' style='width:100%;' rows='3' ></textarea>"; } </script> </head> <body> <table id="MyTable" border="1"> </table> <input type="button" value="添加行并设置属性" onclick="insertRowForIE()" /> </body> |