I have been studying. Net for nearly two years. Recently I learned about Ajax and found that many of my technical skills are lacking, such as JavaScript. This is my first note in the park. In the future, I will write my learning notes as well. If I have time, I will also sort out my previous documents.
1. Introduction to Javascript:
Before learning JavaScript, you need to know something first:
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 consists of several lines of executable computer code.
- 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>
<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:
<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.
-
<SCRIPT type = "text/JavaScript">
Document. Write ("Document. Write ("world"); // output "world"
/*
Such multi-line comments.
*/
</SCRIPT>
In this simple example, we can see that HTML statements can be embedded in Javascript output statements. You can try to write some complex examples.
Iv. Basic JavaScript syntax
1. Declare Variables
VaR X; var x = 1; var x = "hello ".... Just a var, which is very simple.
2. Operators
This is very similar to other languages. Find a table online and check it for yourself.
Operator |
Description |
|
|
+ |
Add |
|
|
- |
Subtraction |
|
|
* |
Multiplication |
|
|
/ |
Division |
|
|
% |
Returns the remainder. |
|
|
++ |
Accumulate |
|
|
-- |
Decrease |
|
|
Operator |
Example |
Equivalent |
= |
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 result of these two strings is output.
3. If, switch, for, while
-
These are all very simple. Just look at the example.
-
<SCRIPT type = "text/JavaScript">
VaR A = 3
If (A = 1)
{
Document. Write ("the value of A is 1 ");
}
Else if (a = 2)
{
Document. Write ("the value of A is 2 ");
}
Else
{
Document. Write ("the value of A is greater than 2 ");
}
</SCRIPT>
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
}
for (i=0;i<=10;i++)
{
document.write("The number is " + i)
document.write("<br />")
}
var i=0
do{
document.write("The number is " + i)
document.write("<br>")
i++
}while (i <= 10)
var i=0
while (i <= 10)
{
document.write("The number is " + i)
document.write("<br>")
i++
}
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.