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>