Summary of common javascript usage

Source: Internet
Author: User

JS decoding and ipv.html
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> javascript encoding and decoding </title>
<Script type = "text/javascript">

Function gel (id ){
Return document. getElementById (id );
}

Window. onload = function (){
// Alert (document. getElementById ("span1"). innerHTML
Gel ("btn1"). onclick = function (){
Alert (encodeURI (gel ("span1"). innerHTML ));
};

Gel ("btn2"). onclick = function (){
Alert (decodeURI (gel ("span1"). innerHTML ));
};
};
</Script>
</Head>
<Body>
<Span id = "span1"> crazy guy! </Span>
<Input type = "button" id = "btn1" value = "encoded"/>
<Input type = "button" id = "btn2" value = "decoded"/>
</Body>
</Html>

Use of setintervaland settimeout.html in JS
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> Use of setInterval and setTimeout in js </title>
<Script type = "text/javascript">
Var time = 10;
Var id = 0;
Function gel (id ){
Return document. getElementById (id );
}

Function dectime (){
If (time> 0 ){
Time --;
Gel ("timespan"). innerHTML = time;
} Else {
// Clear the hour hand
ClearInterval (id );
}
}

Window. onload = function (){
Id = setInterval (dectime, 1000 );
};
</Script>
</Head>
<Body>
<Span> countdown <span id = "timespan" style = "color: red;"> </span> seconds </span>
</Body>
</Html>

JS check that the input field is a number. html
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> js check whether the input is a number </title>
<Script type = "text/javascript">



Window. onload = function (){
Document. getElementById ("btn1"). onclick = function (){
Var I = prompt ("Enter the value to be judged ");
// Window. alert (I );
If (! IsNaN (I )){
Window. alert ("Number ");
} Else {
Window. alert ("not a number ");
}
};
}
</Script>
</Head>
<Body>
<Input type = "button" id = "btn1" value = "Judge Number"/>
</Body>
</Html>

Create and delete node. html
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> js dynamic acquisition, creation, and deletion of nodes </title>
<Script type = "text/javascript">
Function gel (id) {return document. getElementById (id );}

Window. onload = function (){
Gel ("btnProAdd"). onclick = function (){
// Add a subnode under proList
Var linew = document. createElement ("li ");
Linew. innerHTML = prompt ("Enter the province to be added ");
Gel ("proList"). appendChild (linew );
// Rebind all click Delete events
DelLiOnClick ();
};

// Double-click the li subnode and delete it.

Function DelLiOnClick (){
// 1. obtain all the Child Nodes
Var liNodes = gel ("proList"). childNodes;

For (var I = 0; I <liNodes. length; I ++ ){
LiNodes [I]. onclick = function (){
// Alert (liNodes [I]). innerHTML; // because onclick is bound to an anonymous function, I will always be 7
// The following is the correct deletion method. Use this because the onclick event is always triggered by the li
This. parentNode. removeChild (this );
};
}
}


};
</Script>
</Head>
<Body>
<Ul id = "proList">
<Li> Shanxi </li>
<Li> Henan </li>
<Li> Beijing </li>
</Ul>

<Input type = "button" value = "add Province" id = "btnProAdd"/>
</Body>
</Html>

Use of setintervaland settimeout.html in JS

Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> Use of setInterval and setTimeout in js </title>
<Script type = "text/javascript">
Var time = 10;
Var id = 0;
Function gel (id ){
Return document. getElementById (id );
}

Function dectime (){
If (time> 0 ){
Time --;
Gel ("timespan"). innerHTML = time;
} Else {
// Clear the hour hand
ClearInterval (id );
}
}

Window. onload = function (){
Id = setInterval (dectime, 1000 );
};
</Script>
</Head>
<Body>
<Span> countdown <span id = "timespan" style = "color: red;"> </span> seconds </span>
</Body>
</Html>

Jsdynamic topology table data .html
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> dynamically Add Table Data </title>

<Script type = "text/javascript">

Var mailArr = [
{"Title": "A c # question", "name": "Zhang San", "date "},
{"Title": "A javascript question", "name": "Li Si", "date "},
{"Title": "A c problem", "name": "5th", "date "},
{"Title": "A c ++ question", "name": "Zhao six", "date "}
];

Window. onload = function (){
Var tab = document. getElementById ("tb ");
// Add the mailArr looping Method to the table as tr
For (var rowindex = 0; rowindex <mailArr. length; rowindex ++ ){
Var tr = document. createElement ("tr ");
Var th1 = document. createElement ("th ");
Var 2nd = document. createElement ("th ");
Var th3 = document. createElement ("th ");
Var th4 = document. createElement ("th ");
Th1.innerHTML = "<input type = 'checkbox'/> ";
Th2.innerHTML = mailArr [rowindex]. title;
Th3.innerHTML = mailArr [rowindex]. name;
Th4.innerHTML = mailArr [rowindex]. date;

Tr. appendChild (th1 );
Tr. appendChild (2nd );
Tr. appendChild (th3 );
Tr. appendChild (th4 );

Tab. appendChild (tr );
}

};
</Script>

</Head>
<Body>
<Table id = "tb" border = "1px;" style = "border-collapse: collapse;">
<Tr>
<Th> sequence </th>
<Th> title </th>
<Th> mail sender </th>
<Th> sending time </th>
</Tr>
<! -- Add cyclically -->
</Table>
</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.