Js:
Three types of JS:
<!--event: is the user's operation or action, that is, the timing of JS is invoked; For example: Single event, double-click event--
<!--1. Event definition: Write directly to JS--and when defining an event
<input type= "button" value= "BTN1" onclick= "alert (' Dot I do ')"/>
<!--2. Embedded: Write JS in the Script tab this tag can be written anywhere on the page--
JS annotations are the same as Java/**/
The functions in the/*1.js are public and therefore not decorated with modifiers */
The function in/*2.js does not declare the return value type */
function F2 () {
Single and double quotes in JS
Alert ("What's Wrong With You");}
<!--3. File invocation: JS file in JS----
<script src= "My.js" ></script>
JS in:
There are two cases of code written directly
1. Define the function, which is called when the user taps the button after the page is loaded.
2. Directly write JS, such JS is in the page load directly call, and its call time is even earlier than the body. It is possible that some tags are obtained for the defined State;
Alert ("Who makes you open");
Console.log ("Print the console");
1. Declare the variable and output it to the browser console
var x;
Console.log (x);
x = 5;
Console.log (x);
x = "Your Sister";
Console.log (x);
var y = 6;
Console.log (y);
2.. data type
var s = "Hello";
var n = 3.14;
var B = true;
Console.log (s);
Console.log (n);
Console.log (b);
3: Implicit conversion
Console.log (S+n);
Console.log (S+B);
Console.log (N+B);
Console.log (B+B);
4: Cast
Console.log (parseint (3.14));
Console.log (parseint ("5.63"));
Console.log (parseint ("Zxc"));
Console.log (parseint (""));
5.. type Judgment
Console.log (typeof (s));
Console.log (typeof (N));
Console.log (IsNaN (56));
Console.log (IsNaN ("56"));
Console.log (IsNaN ("A56"));
Console.log (IsNaN ("Zxc"));
Console.log (IsNaN (parseint (")));
6. Operators
Console.log (5== "5");
Console.log (5=== "5");
7. Conditions
JS can be used with any data conditions, when the condition is non-boolean, null value equivalent to FALSE, non-null value equivalent to True
Null value: 0,null, "", nan,undefined
The purpose of this design is to simplify the logic of judgment.
var k = 0;
if (k) {Console.log ("OK");}
var w = 6;
W && console.log ("good");
Get tags by id
var input = document.getElementById ("num");
JS Debugging Tips:
1. Reading the wrong information (in the browser console)
2. Piling (track the process of program execution, view values of key step variables)
3. Elimination method (delete half of the code to keep half the code to see if the error, to locate the problem)
JS in the API collation:
1.String objects: The Main method is like Java, look at the manual
2.Number objects
var n = 3.1415926;
Console.log (n.tofixed (3));
3.Array objects
1) How to create an array
var a1 = ["Zhangsan", 25,false];
Console.log (A1[0]);
var a2 = new Array ();
A2.push ("Lisi");
A2.push (28);
A2.push (TRUE);
Console.log (a2[1]);
Conclusion: No matter how the arrays are created in any way,
They are all object arrays and can store any type of data.
2. Reversal method
var arr = [5,12,8,1,6,4,7];
Arr.reverse ();
Console.log (arr);
3. Sorting methods
Since the array in JS is an object array, it is sorted by default by the string from small to large.
Arr.sort ();
Console.log (arr);
Arr.sort (function (b) {
return a-B;
});
Console.log (arr);
4.math--and Java are similar, look at the manual
5.Date
1) How to create a Date object
var D1 = new Date ();
Console.log (D1);
var d2 = new Date ("2017/10/01 10:00:00");
Console.log (D2);
2) How to format the date
Console.log (D1.tolocaledatestring ());
Console.log (D1.tolocaletimestring ());
3) Read/write Time component
var y = d1.getfullyear ();
var m = d1.getmonth () +1;
var d = d1.getdate ();
var h = d1.gethours ();
var mm = D1.getminutes ();
var s = d1.getseconds ();
var now = y+ "year" +m+ "month" +d+ "Day" +h+ "when" +mm+ "sub" +s+ "Seconds";
Console.log (now);
6.RegExp
1) How to create a regular object
var str = "You can up,no can no bb.";
var reg =/no/;
2) method of the regular object: test
Detects if a substring is contained within a string that matches a regular match
Console.log (Reg.test (str));
3) method of the regular object: exec
Normal Mode:
Find the 1th substring that matches the regular match from within a string
Console.log (Reg.exec (str));
Global mode:
1th call, the 1th substring matching the regular is found from within the string
Nth call, the nth substring that matches the regular is found from within the string
reg =/no/g;
Console.log (Reg.exec (str));
Console.log (Reg.exec (str));
Console.log (Reg.exec (str));
Console.log (Reg.exec (str));
4) method to support regular in string
Replace,match,search
Check out the brochure
7.Function objects
Declaring a function, that is, defining a function object
function sum () {
var s = 0;
if (arguments.length) {
for (Var i=0;i<arguments.length;i++) {
s + = arguments[i];
}
}
return s;
}
OnLoad is a page load event that is automatically triggered by the browser when the page load is complete.
Window.onload = function () {
Alert (1);
1. Read the name and type of the node
var P1 = document.getElementById ("P1");
Console.log (P1.nodename);
Console.log (P1.nodetype);
Console.log (Document.nodename);
Console.log (Document.nodetype);
2. Read and write the contents of the node
The text in the middle of the double label is content, such as <a> content </a>
InnerHTML (with label)
var P2 = document.getElementById ("P2");
Console.log (p2.innerhtml);
p2.innerhtml = "2.DOM operation includes <u> queries </u> nodes";
InnerText (Ignore tags)
var p3 = document.getElementById ("p3");
Console.log (P3.innertext);
P3.innertext = "3.DOM operation includes <u> deletions </u> nodes";
3. Read and write the value of the node
Text inside the form control (except label) is a value
var input = document.getElementById ("btn");
Console.log (Input.value);
Input.value = "BBBBB";
4. Properties of the read-write node
1) through the Get/set method (can read and write any property)
var img = document.getElementById ("I1");
Console.log (Img.getattribute ("src"));
Img.setattribute ("src", ".. /images/02.jpg ");
Img.removeattribute ("src");
2) by property name (a few properties can be used in this way)
ID, className, style
var a = document.getElementById ("A1");
Console.log (A.style.color);
A.style.color = "Blue";}
I am a beginner, if the update is not good, welcome the Great God pointed out, thank you!
More exciting after the update, reprinted annotated!
JS at the front of the java-