JavaScript (ii)--grammar

Source: Internet
Author: User

1. Basic data type:

String, Decimal, Integer, DateTime, Boolean, and so on.

2. type conversion:

It is divided into automatic conversion and casting, which is generally used for casting.

Other types are converted to integers: parseint ();

Other types convert to decimals: parsefloat ();

Determine if it is a valid number type: IsNaN ();

is a number then returns false, not a number to return ture.

Example:

var a =prompt ("Please enter a number", "can only be a number!! "); if (a!=null) {    var b =IsNaN (a);     if (b==false)    {        alert ("input correct! ")        }    else    {        alert (" Input Error!! ")        }}</script>
View Code

Run Display:

3. operators:

Math operator : +-*/% + +-;

relational operators : = =! = >= <= > <;

logical operator :&& | | ! ;

Other operators : + = = *=/=%=? :;

4. statement:

It is generally divided into sequential, branching, and cyclic statements.

( 1 ) Branch Statements if :

if (judging condition)

{

Statements that meet the criteria to be executed

}

Else

{

Statements executed when a condition is not met

}

( 2 ) Loop for statement:

for (initial condition; cyclic condition; state change)

{

Loop body

}

(3) Problem type: exhaustive, iterative.

(4) Two keywords: break and continue.

(5) If you want to output the value of a parameter: the value of the output is "+a+" and "+b"

5. Variables:

are generic type Var, can store other types of values, can be used directly, not defined. But it's customary to define.

Defining variables: var A; //All variable definitions are defined with Var, var is a generic mutable type.

var s = "3.14"; var n = parsefloat (s);; s + = 5;
var d = parseint (s);

6. Arrays:

Definition of the array: new Array (); //Its length is dynamically variable and can be placed in any type of element.

Assignment of array elements: a[0]=123;  a[1]= "Hello"; the index in the//element starts at 0.

The value of the array: A[i];

Array properties: A.length; the number of elements in the//array, length.

Methods: A.sort (); //array sorting, sorted by the first character of each element.

A.reverse (); //Flip an array.

7. functions:

Four elements of a function: name, input, return value, processing.

Define functions: Function add (formal parameter) {function body} //function named Add, input as parameter, return value can be var type, or return value.

The function must be called to execute. Call to function: Add (argument).

Example:

1. Enter your gender, height, and weight to see if the criteria are met

2. Enter your name, score, check whether the pass, if >=80, and then output you are excellent! Continue to maintain

3. Set up a DIV, prepare 3 images, set 3 buttons, change the different backgrounds, the default is the first (similar to the QQ Space Change theme)

4. Set a P tag, and when you click the position of the P tag, change the color of the text, change the size of the text

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">font{Font-weight:700;}</style>Sex (male/female): <input id= "Sex" type= "text"/><br/> Height (cm): <input id= "height" type= "text"/><br/>weight (kg):<input id= "Weight" type= "text"/><br/><input type= "button" value= "Check whether standard" onclick= "Check ()"/>< br/><br/><font size= "+3" >2. Enter your name, score, check whether the pass, if >=80, then output you are very good! Continue to keep </font><br/>Name:<input id= "name" type= "text"/><br/>Score:<input id= "Score" type= "text"/><br/><input type= "button" value= "See if Pass" onclick= "Chakan ()"/>< br/><br/><font size= "+3" >3. Set up a DIV, prepare 3 images, set 3 buttons, change different backgrounds respectively, default is the first (similar to the QQ Space change theme) </font>< BR/><div id= "D" style= "width:360px; height:240px; border:1px solid red; Background-image:url (123.jpg) "></div><br/><input type=" button "onmouseover=" Change1 () "Value=" First theme "/> <input type=" button "onmouseover=" Change2 () "value=" Second topic "/> <input type=" button "onmouseover=" Change3 () "Value=" the third theme "/><br/><br/><font size=" +3 ">4. Set a P tag, change the color of the text when you click the position of the P tag, change the size of the text </font><br/><p id= "P1" onclick= "Color1 ()" > Looking forward, looking forward, the East wind came, spring's footsteps near. </p><p id= "P2" onclick= "Color2 ()" > Everything was just like waking up, and pleasantly opened his eyes. The mountain Lang run up, the water rose up, the sun blushed up. </p><p id= "P3" onclick= "Color3 ()" > Grass secretly from the land drilling out, tender, green and green. In the garden, in the fields, look, a big big one. Sit, lie down, play two roll, kick a few kicks, race a few times to run, catch a few mystery. The wind is soft, the grass is limp. </p></body>functionCheck () {varSex = document.getElementById ("Sex"). Value; varHeight = parseint (document.getElementById ("height")). Value); varWeight = parseint (document.getElementById ("Weight"). Value); if(sex== "Male")    {        if(weight) {alert ("The weight is thin, eat a bit more!" "); }        Else if(weight>height-100+3) {alert ("You are too fat, exercise more!" "); }        Else{alert ("Weight is standard, keep it up!" ")    ; }        }        Else if(sex== "Female")    {        if(weight) {alert ("The weight is thin, eat a bit more!" "); }        Else if(weight>height-110+3) {alert ("You are too fat, exercise more!" "); }        Else{alert ("Weight is standard, keep it up!" ")    ; }            }    Else{alert ("Is it a man or a woman?" "); }}functionChakan () {varName = document.getElementById ("name"). Value; varScore = parsefloat (document.getElementById ("Score"). Value); if(Score>=0 && score<=100)    {        if(score<60) {Alert (name+ "Failed"); }        Else        {            if(score>=80) {alert ("Congratulations," +name+ ", passed, and you are excellent, continue to keep! "); }            Else{alert (name+ "Passed"); }        }    }    Else{alert ("Score input is wrong!" "); }}functionChange1 () {varD = document.getElementById ("D"); D.style.backgroundimage= "url (123.jpg)";}functionChange2 () {varD = document.getElementById ("D"); D.style.backgroundimage= "url (213.jpg)";}functionChange3 () {varD = document.getElementById ("D"); D.style.backgroundimage= "url (312.jpg)";}functionColor1 () {varP1 = document.getElementById ("P1"); P1.style.color= "#3F3"; P1.style.fontSize= "30px"; }functionColor2 () {varP2 = document.getElementById ("P2"); P2.style.color= "#FF3"; P2.style.fontSize= "30px"; }functionColor3 () {varP3 = document.getElementById ("p3"); P3.style.color= "Red"; P3.style.fontSize= "30px"; }</script>
View Code

Run the effect shows here is not attached, interested in the words copy the code, run the following to see your own.

Note: * can be read on the head (first read the head)

The page is automatically refreshed when the form is submitted, preferably turned off

Writing:

1. Output

<script>

document.write ("Hello World");

</script>

2. Button Application

<input name= "alert" value= "alert" type= "button" onclick= ' Alert ("This is the trigger event") '/>

3. Define variables and output

var Bianliang;

Bianliang= "123";

document.write (Bianliang);

4. Changing the attributes of an element

<p id= "Hello" >hello</p>

<script>

function Hello ()

{

X=document.getelementbyid ("Hello")

X.style.color = "#ff0000";

}

</script>

<input type= "button" value= "Click to change Color" onclick= "hello ()"/>

<!--click From Black to red--

5 , Item index, indicating the first N an element that appears, with the Getelementsbyname simultaneous use

6. Define a set of variables

var student =

{

Name: "Zhang San",

Sex: "Male"

};

Student.name= "Zhang San";

document.write (Student.name);

(* print "Zhang San")

7. External Call JS

<script src= "" ></script>

JavaScript (ii)--grammar

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.