1. Events
In the hyperlink click, execute javascript:<A Href= "Javascript: Alert ('haha ')">Haha</A>
The event concept is also available in Javascript. It can also be executed when a button is clicked.
Javascript:
• <Input type = "button" onclick = "alert (99)" value = "long time"/>
• "Javascript:" is required only for the Javascript in the href of the hyperlink, because it is not an event, but "" javascript: "is regarded as" http: "and" ftp: "," Thunder: // "," ed2k: // ", and" mailto: "are handled by the JS parsing engine. Only href. This is a special case.
2. Variables
(1) A string can be declared using double quotation marks in JavaScript,You can also use single quotes to declare strings.. It is mainly used to facilitate HTML integration and avoid the trouble of escape characters.
(2) In JavaScriptNull,UndefinedNull indicates that the value of the variable is null, and undefined indicates that the variable has not pointed to any object and has not been initialized. Both
.
(3) JavaScript is of a weak type. When declaring a variable, it cannot: int I = 0; it can only be declared through var I = 0; it is different from VAR in C, not the type in C #
Inference.
(4) In JavaScript, variables can also be declared without VaR and used directly. Such variables are "global variables", so it is best to use them unless you really want to use global variables.
Add var.
(5) JS is dynamic, soVaR I = 0; I = "ABC"; is valid.
3. Judge variable Initialization Hide row number CopyCode ? This is Program Code.
-
<ScriptType= "Text/JavaScript">
// Alert (new date (). tolocaledatestring ());
-
VaRX = 0;
-
If(X =Null){
-
Alert ("Null");
-
}
If(Typeof(X) ="Undefined"){
-
Alert ('Undefined');
-
}
If(! X) {alert ('No x');}
-
</Script>
If (x) {} // The variable is initialized, the variable is not empty, or the variable is not 0. The last method is recommended.
4. function declaration
(1) how to declare a function in javascript:
Function add (I1, I2 ){
Return I1 + I2;
}
Int add (INT I1, int I2) // C # statement
(2)The Return Value Type and parameter type do not need to be declared.. Function Definition starts with function.
VaR r = add (1, 2 );
Alert (R );
R = add ("hello", "Tom ");
Alert (R );
(3) JavaScript does not require that all paths have return values as in C #. If no return value is returned, it is undefined.
(4) Easy to error: Do not use a custom function name with the built-in JS or DOM methods, such as selectall or focus.
5. Anonymous Functions
VaR F1 =Function(I1, I2 ){
ReturnI1 + I2;
}
Alert (F1 (1, 2 ));
(1) It is similar to the anonymous function in C.
(2) This anonymous function is used in many jquery functions.
(3) Alert (Function(I1, I2 ){ReturnI1 + I2 ;}(); // directly declare an anonymous function and use it immediately. Using anonymous functions saves you the need to define a function that is not used once, and avoids naming conflicts. js does not have the namespace concept, so it is easy to conflict function names. The example shows that the final declaration is correct once a name conflict occurs.
(4) It must be <SCRIPT src = "my1.js" type = "text/JavaScript"> </SCRIPT> and cannot: <SCRIPT src = "my1.js" type = "text/JavaScript"/>