Javascript small function (operate Table -- dynamically add and delete tables and data) to implement code

Source: Internet
Author: User

Effect:

 

Code:
Copy codeThe Code is as follows:
<Head runat = "server">
<Title> </title>
<Style type = "text/css">
Tr
{
Height: 30px;
}
</Style>
<Script type = "text/javascript">
Window. onload = function (){
Var oName = document. getElementById ('txt1 ');
Var oEasyName = document. getElementById ('txt2 ');
Var oHero = document. getElementById ('txt3 ');
Var oBtn = document. getElementById ('btn ');
Var oTab = document. getElementById ('tab1 ');
Var num = oTab. tBodies [0]. rows. length + 1;
OBtn. onclick = function (){
Var oTr = document. createElement ('tr ');

Var oTd = document. createElement ('td ')
OTd. innerHTML = num ++;
OTr. appendChild (oTd );

Var oTd = document. createElement ('td ');
OTd. innerHTML = oName. value;
OTr. appendChild (oTd );

Var oTd = document. createElement ('td ');
OTd. innerHTML = oEasyName. value;
OTr. appendChild (oTd );

Var oTd = document. createElement ('td ');
OTd. innerHTML = oHero. value;
OTr. appendChild (oTd );

Var oTd = document. createElement ('td ');
OTd. innerHTML = '<a href = "#"> Delete </a> ';
OTr. appendChild (oTd );
OTd. getElementsByTagName ('A') [0]. onclick = function (){
OTab. tBodies [0]. removeChild (this. parentNode. parentNode );
}
OTab. tBodies [0]. appendChild (oTr );
}
};
</Script>
</Head>
<Body>
<Div style = "margin-left: 300px; margin-top: 30px;">
Race name: <input type = "text" id = "txt1"/>
Race Abbreviation: <input type = "text" id = "txt2"/>
Hero: <input type = "text" id = "txt3"/>
<Input type = "button" id = "btn" value = "add information"/>
</Div>
<Table id = "tab1" border = "1" style = "text-align: center; width: 800px; margin-left: 300px;
Margin-top: 10px; ">
<Thead>
<Tr style = "background-color: # FF0000">
<Td>
Serial number
</Td>
<Td>
Race name
</Td>
<Td>
Race abbreviation
</Td>
<Td>
Hero
</Td>
<Td>
Operation
</Td>
</Tr>
</Thead>
<Tbody>
<Tr>
<Td>
1
</Td>
<Td>
Alliance
</Td>
<Td>
HUM
</Td>
<Td>
Representative hero: AM
</Td>
<Td>
</Td>
</Tr>
<Tr>
<Td>
2
</Td>
<Td>
ORC tribe
</Td>
<Td>
ORC
</Td>
<Td>
Representative hero: BM
</Td>
<Td>
</Td>
</Tr>
<Tr>
<Td>
3
</Td>
<Td>
Undead
</Td>
<Td>
UD
</Td>
<Td>
Representative hero: DK
</Td>
<Td>
</Td>
</Tr>
<Tr>
<Td>
4
</Td>
<Td>
Night genie
</Td>
<Td>
NE
</Td>
<Td>
Representative hero: DH
</Td>
<Td>
</Td>
</Tr>
</Tbody>
</Table>
</Body>

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.