JavaScript Basics (a) What is JavaScript
- Page-based scripting language
- Lightweight
- can be inserted into HTML
- Basically, all browsers support
- Completely different from Java
Label
The browser executes the script inside the tag.
<script></script>
Position
This script is habitually placed underneath the head or inside the body.
But more still in a single file, and then import the file
<script src="myScript.js"></script>
Looking for a certain ID
JavaScript determines the specific label by looking for an ID in the HTML
document.getElementById("demo").innerHTML="我的第一段 JavaScript";
The above code can replace the original contents of the demo tag with "My first JavaScript."
document.write("<p>我的第一段 JavaScript</p>");
The above code prints a paragraph to the screen, note! It can only be used when the page has just been loaded, and will overwrite the page if it is loaded.
Branch
Semicolons are optional for Javascrip
Function
Functions need to be added in front of function
function functionName(){}
Code line Wrapping
Not recommended, you can use \ Wrap between strings
Comments
The comments are exactly the same as the C/////* *
Variable
Use Var to declare variables
Array
var a = new Arry(‘q‘, ‘w‘, ‘e‘, ‘r‘)
Object oriented
JavaScript is an object-oriented language, and all variables are objects
var person={firstname : "Bill",lastname : "Gates",id : 5566}
Recommended when working with properties in an object
person.firstname
Recommended when working with methods in an object
person.eat()
Calling functions
Many tags can be added onclick= "MyFunction ()"
This allows you to call functions
return value
function can be returned without specifying a function type
Operators, comparators, logical operators
+-*,%<>= = = = >= <= && | | !
The meanings of all symbols are the same as ordinary
Basic statement
if () {}else{}
Switch () {case1:break;default:break;}
for (;;)
While
Break;continue;
Try throw catch
Form validation
Use ID to read value for further verification
Alert () can output a warning
Console.log () can print logs
JavaScript Basics (i)