Use of table object programming based on objective-dom

Source: Internet
Author: User

Ranking exercise: add data to a table. When the number is repeated, you are prompted that the data cannot be added, and you can add data in the ranking order.

Reference code:

Copy codeThe Code is as follows: <! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">

<Html>
<Head>
<Title> ggggg </title>
<Link rel = "stylesheet" type = "text/css" href = "">
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
<! -- JavaScript code can be placed in any position and executed sequentially among head labels. -->
<Script type = "text/javascript">

Function test (){

// Obtain the table object
Var mytable = document. getElementById ("table1 ");
// Determine whether to insert a duplicate ranking
// Traverse
// Define the position of the variable to be inserted
Var insertW = mytable. rows. length;
For (var I = 1; I <mytable. rows. length; I ++ ){
// Retrieve each row
Var eRows = mytable. rows [I];
// Judge whether the input ranking is repeated
If (eRows. cells [0]. innerText = no. value ){
Window. alert ("with ranking" + eRows. cells [0]. innerText + "repetition ");
Return; // block code execution below
}

// Sort and find the location to insert
If (parseInt (no. value)> parseInt (eRows. cells [0]. innerText )){
InsertW = I;
// Window. alert (insertW );
}
// Window. alert ("OK ");
}

// Insert after sorting
Var eachRow = mytable. insertRow (insertW + 1 );
// Add data to each row
EachRow. insertCell (0). innerText = document. getElementById ("no"). value;
EachRow. insertCell (1). innerText = username. value;
EachRow. insertCell (2). innerText = nickname. value;

}

</Script>
</Head>
<Body>
<Table id = "table1" border = "1">
<Tr> <td> ranking </td> <td> name </td> <td> nickname </td> </tr>
<Tr> <td> 1 </td> <td> Song Jiang </td> <td> timely rain </td> </tr>
<Tr> <td> 2 </td> <td> Lu Junyi </td> <td> Yu Qilin </td> </tr>
<Tr> <td> 10 </td> <td> test </td> </tr>
</Table>
<Br/>
Ranking: <input id = "no" type = "text" name = "no"> <br/>
Name: <input id = "username" type = "text" name = "username"> <br/>
Nickname: <input id = "nickname" type = "text" name = "nickname"> <br/>
<Input type = "button" value = "add" onclick = "test ()"> <br/>
</Body>
</Html>

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.