JavaScript Talbe Table Inserts a row of implementation code script home modified version _javascript tips

Source: Internet
Author: User
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> &LT;/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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.