JavaScript Basics Grooming (1)

Source: Internet
Author: User
Tags try catch

A rough understanding of an effort to get started

1. Introduction of external script in HTML: <script src= "Filename.js" ></script>

2. Note: Long comments with more than one line use the/* Comment content */, only a single line of comments with//comment content.

3. Create the format of the function:

Function name () {

The contents of the function

}

A variable created inside a function is a local variable that is valid only within the function, and the variable created outside of all functions is a global variable that can be used anywhere in the code.

4. getElementById () obtain and manipulate elements by ID;

Getelementbyvalue () acquiring and manipulating elements through value

5. Dot syntax: Object. Properties. Method

6. The keyword this points to the object, which can be used to pass values to the function using its context. The context in most cases refers to the object that encapsulates this function when the function is run.

9, if () ... else ... Statement

if (condition) {

Statement executed when the condition is true

}

else{

Statement executed when the condition is false

}

10. Switch (variable) {

Case value 1:

The statement that is executed when the value of the variable variable is 1

Break

Case Value 2:

The statement that is executed when the value of the variable variable is 1

Break

Case Value 3:

The statement that is executed when the value of the variable variable is 1

Break

.

.

.

Case Value N:

The statement that is executed when the value of the variable variable is 1

Break

Default: Statement executed when the value of the variable variable does not match any of the above values

}

Break: When the value of a variable is equal to the value following the case, leave the switch statement, or continue execution down.

11. Try Catch Throw

This part of the content in the following links have more detailed explanation http://www.w3school.com.cn/js/js_errors.asp

12. For loop

For example: for (var i=0;i< a value; i++) {

code block (as long as the counter variable i< a value, loop through this block of code)

}

13. Arrays

For example:

var baiyu=new Array ("Xiang", "Yu", "Yong", "Shang"); Declaring a string array

var number=new array (1,3,4,6,7,5,3,2);//declaring a numeric array

var bai=new array (66);//declares a new array containing 66 objects

To call an array in the form of an array name []

14, do not know how many times the code loop when using the Do/while loop

do{

Code Block 1

}

while (code block 2);

Loop executes code block 1 as long as the result of code block 2 is true.

15. Image Processing

Made a fun flip-up (though the general flip-out is not used)

Effect:

HTML code: <! DOCTYPE html>
<link href= "Bear.css" rel= "stylesheet" type= "Text/css" >
<script src= "Bear.js" ></script>
<body>
<a href= "lefteye.html" ></a>
<a href= "right.html" ></a>
</body>

CSS code:

body{
width:600px;
height:400px;
margin:0 Auto;
margin-top:120px;
}
img{
margin:0;
Float:left;
}

JavaScript code:

Window.onload=initeyes;

function Initeyes () {

document.getElementById ("left"). Onmouseover=leftopen;

document.getElementById ("left"). Onmouseout=leftclose;

document.getElementById ("right"). Onmouseover=rightopen;

document.getElementById ("right"). Onmouseout=rightclose;

}

function Leftopen () {

This.src= "Images/leftopen.gif";

}

function Leftclose () {

This.src= "Images/leftclose.gif";

}

function Rightopen () {

This.src= "Images/righttopen.gif"; }

function Rightclose () {

This.src= "Images/rightclose.gif"; }

A lot of the repetitive parts of this JavaScript code might be able to make the code more concise in a way like a template class in C + +. Looking forward to the next study.

PS: The picture used is made by the plate +ps.

JavaScript Basics Grooming (1)

Related Article

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.