Implementation of inserting a row at a specified position in the JavaScript talbe table

Source: Internet
Author: User

Recently I have been learning JavaScript. The following is an example (based on the w3cschool example)
In this example, a row is inserted after the row clicked by the mouse. By default, a row is inserted in the first row. You can enter the inserted content by yourself. The Code is as follows:
<Html> <pead> </pead> <body onmousedown = "whichElement (event) "> <table id =" myTable "border =" 1 "> <tr> <td> Row1 cell1 </td> <td> Row1 cell2 </td> </tr> <tr> <td> Row2 cell1 </td> <td> Row2 cell2 </td> </tr> <td> Row3 cell1 </td> <td> row3 cell2 </td> </tr> </table> <p> Cell1: <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 select all Note: If you need to introduce external Js, You need to refresh it to execute]

The content above is inserted to the start position by default.
Note that when you click the insert row button, the action also captures and changes the global variable. At first, I didn't realize that the above red code was not added, therefore, every time you click the insert row, it is inserted in the first line. Originally, according to the original design, When you click the button, the global variable is changed to 0, so it can only be added in the first line.
The simple solution is to add the red code to avoid the problem just now.
The above is only a small learning experience. You can use this as a template, such as deleting a specified row or inserting a row before it.
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.