It is planned to finish the javascript tutorial and organize your notes before November 5
Run on HTML
<script> javaScript content </script>
The part between the tags < script > < / script > is the javaScript content
For HTML pages, generally speaking, the content between the head tags plays the role of page attribute specification. The content will not be displayed on the page, but the script in the head tag will be displayed on the page if printed
For example:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>a hallowin from deutschBall</title> <script> document.write("new page");//The script has output </script> </head> <body></body> </html>
new page is displayed on the HTML page
If the script part has no output, the script will not be displayed on the HTML page
If the script output is after the HTML page has been loaded, the script output will overwrite the HTML page
For example:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>a hallowin from deutschBall</title> <script> function fresh(){ document.write("new page"); } </script> </head> <body> <h1>Primary title 1</h1> <button onclick="fresh()">click</button> <h1>Primary title 2</h1> </body> </html>
click:
It is found that the page has been updated and the printed content in the function is output
Scripts are usually written to the < head > section
External script:
For example, there is a test.js script file locally, and its absolute directory is C:\Users\Administrator\Desktop\javaScript\test.js
test.js:
document.write("External script");
The HTML page reads:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>a hallowin from deutschBall</title> <script src="C:\Users\Administrator\Desktop\javaScript\test.js"></script> </head> <body> </body> </html>
Then the operation result is:
External script
output
window.alert()
Output to warning box
<!DOCUTYPE html> <html> <head> <script> window.alert(Date()); </script> <head> </html>
Operation results:
document.write()
Output to page
innerHTML
Output to HTML elements (fill or replace element values)
<!DOCUTYPE html> <html> <head> <script> </script> <head> <body> <p id="para">to be replace</p> <script> document.getElementById("demo").innerHTML = "replaced"; </script> </body> </html>
Operation results:
replaced
Explanation:
document.getElementById("demo").innerHTML = "replaced";
Where document refers to all HTML page contents
getElementById refers to the element obtained through the Id attribute, and the function parameter is the Id value. If there is an element with Id value matching on the HTML page, the function returns the element,
innerHTML feels like a reference to the HTML value of the element just returned (value between element tags < tag > value < / tag >)
console.log()
Output to console
It will not be output to the HTML page, but can be output to the console during the test
grammar
data type
j a v a S c r i p t number according to class type { value class type { number word word symbol strand cloth Er empty not set righteousness S y m b o l lead use class type { number group yes as letter number javaScript data type \ begin value type \ begin number \ \ string \ \ Boolean \ \ empty \ \ undefined \ \ Symbol \end \ \ reference type \ begin array \ \ object \ \ function \ \ end \end The javaScript JavaScript javaScript data types should be in the variety of the javajavascript javaScript JavaScript javaScript JavaScript javaScript JavaScript should should should be the one one of the one of the one of the one of the the one of the the one of the the the one of the javaScript JavaScript javaScript JavaScript javaScript JavaScript data types the please please please please please please please please please please please please please please please please please please please please please please please please please please please please please submit the javaScript JavaScript javaScript JavaScript javaScript JavaScript javaScript JavaScript javaScript JavaScript javaScript data types the one of the types of the one of the one of the the javaScript JavaScript javaScript JavaScript javaScript JavaScript javaScript JavaScript javaScript data types the one of the more more more the one of the one of the one of the more more more more more more more more more more more more more more more more more more more more one of the javaScript JavaScript javaScript JavaScript javaScript JavaScript javaScript JavaScript javaScript JavaScript javaScript JavaScript javaScript JavaScript javaScript JavaScript javaScript JavaScript javaScript JavaScript javaScript JavaScript javaScript JavaScript should should should be the one of the one of the one of the one of the one of the one of the one of the one of the one of the one of the one of the one of the one of the one of the 30;⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎪⎧ numeric string Boolean null undefined Symbol reference type ⎩⎪⎨⎪⎧ array object function
The value type size is fixed, and the value is saved in the stack and accessed by value
The size of reference type is not fixed. Handle is stored in stack and object is stored in heap
variableJavaScript variables are objects. When you declare a variable, a new object is created---- Rookie tutorial
Any variable, including value type and reference type, is defined with the keyword var, and the equal sign = assignment
For example:
var x=1.2; var y="Vader";
Variables can be defined multiple times, such as:
var x=3.4; var x="str";
At this time, the variable x retains the last assigned value
var x=3.4; var x;
The second time there is no assignment, so x retains the last assignment
If you only declare that no initial value is assigned, the value is unsigned
Keywords and reserved words are roughly similar to those in java and C + +
typeof keyword to get the variable type:
<!DOCUTYPE html> <html> <head> <script> var arr=[1,2,3,4,5]; var obj=; var str="Vader"; var Int=19; var udef; var bool=true; function func(){} </script> <head> <body> <script> document.write(typeof(arr)+"\r"); document.write(typeof(obj)+"\r"); document.write(typeof(str)+"\r"); document.write(typeof(Int)+"\r"); document.write(typeof(udef)+"\r"); document.write(typeof(bool)+"\r"); document.write(typeof(func)+"\r"); </script> </body> </html>
Operation results:
object object string number undefined boolean function
Arrays are also object s
Elements in the same array can be of different types
<!DOCUTYPE html> <html> <head> <script> var arr=[1,2,3,4,5]; </script> <head> <body> <script> arr[0]="Vader"; document.write(arr[0]); document.write(arr[1]+arr[2]); </script> </body> </html>
Operation results:
Vader5
Since the value type in javascript, regardless of the integer floating-point string, occupies 8 bytes, the offset when accessing the array subscript is fixed
Variable scope and lifetime:
Local variable: a variable declared within a function, which is deleted when the page is closed
Global variables: variables declared in places other than functions are deleted after function calls
Variables assigned directly without var declaration
Variables assigned directly without declaration will be regarded as an attribute of window
window is the browser object model, which exists by default
Global variables cannot be deleted by delete (variables are automatically recycled, and programmers cannot control their demise), but the properties of window can be deleted
About deleting object attributes:
delete object.key;
<!DOCUTYPE html> <html> <head> <script> var vector={ x:10, y:5, z:15, toString:function(){ return "("+this.x+","+this.y+","+this.z+")"; } } </script> <head> <body> <script> document.write(vector.toString()); delete vector.z; document.write(vector.toString()); </script> </body> </html>
Operation results:
(10,5,15)(10,5,undefined)
If you delete a global variable:
<!DOCUTYPE html> <html> <head> <script> var sth=10;//Note that adding var declaration is the global variable, otherwise it is the attribute of window object </script> <head> <body> <script> document.write(sth+"\r"); delete sth; document.write(sth); </script> </body> </html>
Operation results
10 10object
<!DOCUTYPE html> <html> <head> <script> var vader=; </script> <head> <body> <script> document.write(vader.name+" from "+vader.unit); </script> </body> </html>
Operation results:
Vader from the first galactic Empire
The attributes described by various key value pairs are in curly brackets. The writing method is as follows:
key:number_value key:"string_value"
Each key value pair is separated by commas
Define and use structures that are more like C
Call object properties:
<script> document.write(vader.name+" from "+vader.unit+"\r"); document.write(vader["name"]+" from "+vader["unit"]+"\r"); </script>
Operation results:
Vader from the first galactic Empire Vader from the first galactic Empire
object.Attribute key object["Attribute key"]
Object method:
<!DOCUTYPE html> <html> <head> <script> var Complex={ x:3, y:4, printf:function(){ document.write("("+this.x+","+this.y+")"); } }; </script> <head> <body> <script> Complex.x=3; Complex.y=4; document.write(Complex.printf+"\r"); document.write(Complex.printf()); </script> </body> </html>
Method name:function(){//In fact, function is also regarded as a variable Method body }
Note that the member variable in the method body should use the this. Keyword
If parentheses are not used during printing, the function content will be printed directly, and the function will not be executed until parentheses are added after the method name
document.write(Complex.printf+"\r"); document.write(Complex.printf());
Operation results:
function(){ document.write("("+this.x+","+this.y+")"); } (3,4)undefined
Statement identifiers are similar to C + + and Java
functionA function is a reusable block of code that is event driven or executed when it is called.
The keyword function is used to declare the function
function Function name(Parameter table){ Function body (May have return value) }
For example:
Numerical solution of finding function zero by bisection with javaScript
<!DOCUTYPE html> <html> <head> <head> <body> <p id="toChange">before change</p> <button onclick="getElementById('toChange').innerHTML='changed';this.innerHTML='clicked' ">click me</button> <script> document.write(sth+"\r"); delete sth; document.write(sth); </script> </body> </html>
event
HTML events: events that occur on HTML elements, including browser behavior (such as loading) and user behavior (such as clicking a button)
For example:
<!DOCUTYPE html> <html> <head> <head> <body> <p id="toChange">before change</p> <button onclick="getElementById('toChange').innerHTML='changed' ">click me</button> <script> document.write(sth+"\r"); delete sth; document.write(sth); </script> </body> </html>
Occurs when a button is clicked