1. Introduction to javascript:
Before learning JavaScript, you need to know something first:
Html
XHTML
I don't think it should be too deep. Just give me a rough idea.
Javascript is:
Javascript is designed to add interactions to HTML pages.
Javascript is a scripting language.
Javascript is executed by several lines of computersCode.
Javascript is usually directly embedded into HTML pages.
Javascript is an explanatory language.
All users do not need to buy a license to use JavaScript.
These are the definitions of Javascript in some books.
2. Simple JavaScript implementation example:
[HTML]
<HTML>
<Body>
<SCRIPT type = "text/JavaScript">
Document. Write ("this is Javascript .");
</SCRIPT>
</Body>
</Html>
When embedding JavaScript code into an HTML page, you need to add JavaScript labels at the header and tail to tell the browser that this is JavaScript code.
For example, the above example will be displayed on the page:
This is JavaScript.
If there is no red code in the example, it will display:
Document. Write ("this is Javascript .")
In the past, when the old browser did not support JavaScript, to prevent the browser from outputting JavaScript code as text, we can write the code as follows:
Copy code The Code is as follows: <SCRIPT type = "text/JavaScript">
<! --
Document. Write ("Hello world! ");
// -->
</SCRIPT>
In this case, browsers that do not support JavaScript will automatically skip the code and execute the code.
Generally, we try our best to separate JavaScript code into external files. One is to make the Page code less confusing, and the other is to avoid repeated writing when JavaScript code is reused on different pages.
<SCRIPT src = "AAA. js">... </SCRIPT>
. Js files are generally stored in subdirectories of the website to facilitate maintenance and increase code readability.
Iii. Javascript statements and comments
A JavaScript statement can contain ";" or ";". Multiple statements can be written in the same line.Copy codeThe Code is as follows: <SCRIPT type = "text/JavaScript">
Document. Write ("Document. Write ("world"); // output "world"
. */
</SCRIPT>
in this simple example, we can see that HTML statements can be embedded in Javascript output statements. You can try to write complex examples.
4. basic JavaScript syntax
1. declare the variable
var X; var x = 1; var x = "hello ".... Just a var, which is very simple.
2. Operator
similar to other languages, there is nothing to say. Find a table online and check it for yourself.
operator description
+ plus
-minus
* multiplication
/Division
% evaluate the remainder
+ + accumulate
-- decrease
the operator example is equivalent to
= x = Y
+ = x + = Y x = x + y
-= x-= Y x = x-y
* = x * = Y x = x * Y
/= x/= Y x = x/y
% = x % = Y x = x % Y
interestingly, if two strings are added together, the output result is the concatenation of the two strings.
3.if, switch, for, while
all these are simple. Just look at the example. copy Code the code is as follows:
Copy codeThe Code is as follows: Switch (N)
{
Case 1:
Execution Code Block 1
Break
Case 2:
Execution Code Block 2
Break
Default:
If n is neither 1 nor 2, execute this code
}
Copy codeThe Code is as follows: for (I = 0; I <= 10; I ++)
{
Document. Write ("the number is" + I)
Document. Write ("<br/> ")
}
Copy codeCode: var I = 0
Do {
Document. Write ("the number is" + I)
Document. Write ("<br> ")
I ++
} While (I <= 10)
Copy codeCode: var I = 0
While (I <= 10)
{
Document. Write ("the number is" + I)
Document. Write ("<br> ")
I ++
}
Copy codeThe Code is as follows: for (variable in object) // traverses the commonly used
{
Run the code here
}
These are the most basic things. Next time I will write common events in JavaScript.