Recently learning JavaScript, here's an example of myself (based on the W3cschool example)
The example implementation function is to insert a row after the mouse clicks, and the default is to insert the first line. The inserted content can be entered by itself, with the following code
<ptml> <pead> <script type= "Text/javascript" >//global var var pos =0//to Find the position you m Ouse has pressed function whichelement (e) {var targ if (!e) var e = window.event if (e.target) Targ = E.target Else if (e.srcelement) Targ = E.srcelement if (Targ.nodetype = = 3)//defeat Safari bug Targ = Targ.parentnode if (targ.tagn ame== "TD") {//to Adjust whether the element is tabledata pos = targ.parentnode.rowindex+1} else if (targ.tagname==) INPUT ") {//to do no} else{pos =0}//alert (POS)}//to insert a row in the place function Insrow () {//alert P OS) var X=document.getelementbyid (' myTable '). InsertRow (POS) var y=x.insertcell (0) var Z=x.insertcell (1) y.innerhtml=d Ocument.getelementbyid ("Cell1"). Value Z.innerhtml=document.getelementbyid ("Cell2"). Value} </script> </ head> <body onmousedown= "Whichelement (event)" > <table id= "myTable" border= "1" > <tr> <td>ro W1 cell1</td> <td>rOw1 cell2</td> </tr> <tr> <td>row2 cell1</td> <td>row2 cell2</td> </TR&G T <tr> <td>row3 cell1</td> <td>row3 cell2</td> </tr> </table> <p> C Ell1:<input type= "text" id= "Cell1" value= "Cell1"/> cell2:<input type= "text" id= "cell2" value= "Cell2"/> ;/p> <input type= "button" onclick= "Insrow ()" value= "Insert Row" > </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
The above is the content, the default is to insert to the start position.
The point to note here is that when the mouse clicks the Insert Row button, action will also capture and change the global variable, at first I did not realize that, that is, did not add the above red code, so each click Insert Row is inserted in the first line, originally according to the original design, click the button when the global variable to 0, so only in the first row added.
The simple solution is to add the red code, which is to avoid the problem just now.
The above is only a small experience in the learning process, you can use this as a template, such as deleting the specified row, or before the specified line to insert functions can be completed.
Continue to learn JavaScript and continue to communicate.