What are variables?
Variables are containers used to store information
Variable declaration
grammar:
var variable name
variable name = value;
Variables must be declared first and then assigned
Variables can be assigned repeatedly
Naming rules for variables
Variables must start with letters;
Variables can also start with the $ and _ symbols (but we do not recommend this);
Variable names are case sensitive (a and A are different variables).
Statement
The statement ends with a semicolon; if the semicolon is omitted, the end of the statement is determined by the parser.
Have a good coding habit, you must end with;
Data Type
In JavaScript , a piece of information is a value . There are different types of values, and the type that everyone is most familiar with is numbers. The string value is surrounded by
One or more words in quotes.
Number any numeric value. Numbers can be decimal or without 68.57
Characters in string quotation marks. You can use single or double quotes "hello, world"
Boolean true or false true
Undefined and Null Undefined values indicate that the variable does not contain a value. You can clear the variable by setting the value of the variable to null.
Any value associated with an object
The value returned by the function function
1 var a; //a is undefined2 var a = 6; //a is the number 3 var a = "Jason"; //a is a string
What is a function?
Functions are a set of JavaScript statements that perform a certain task.
Basic syntax:
function function name(){ function code;}Function name();
illustrate:
function defines function keywords.
"Function Name" The name you gave to the function.
"Function Code" is replaced with code that completes a specific function.
A function call of "second function name"
1 function add2(){2 var sun = 3 + 2;3 alert(sun);4 }5 add2();//Calling the function directly writes the function name and directly pops up the function code1 <input type="button" value="click me" onclick="add2()" />2 <!-- After clicking the button, call the function, onclick is the click event-->
Output content (document.write)
document.write() outputs content directly on the web page.
The first type: The output content is enclosed with "" and directly output the content within the "" sign.
document.write("I love JavaScript!");The second type: output content through variables
var mystr = "hello world";document.write(mysrt);//Write the variable name directly and output the content stored by the variable
The third type: output multiple contents, and connect them with + signs.
var mystr = "hello";document.write(mystr + "I love Java Script");//Connect multiple items with + signs
The fourth type: output HTML tags and work, and the tags are enclosed with "".
var mystr="hello";document.write(mystr+"<br>");//After output hello, output a newline document.write("JavaScript");Warning (alert message dialog)
When we visit the website, sometimes a small window will pop up with a message text on it. If you don't click "OK", you can't do anything about the webpage
How to operate, this small window is implemented using alert.
Syntax: alert (string or variable);
var mynum = 30;alert("hello!");alert(mynum);Result: The message box pops up in order (the alert pops up message dialog box contains an OK button)
Notice:
1. No other operations can be performed before clicking the "OK" button in the dialog box.
2. The message dialog box can usually be used to debug a program.
3. Alert output content, which can be a string or a variable, similar to document.write
Confirm selection (confirm message dialog)
In addition to providing information to users, we also want to obtain information from users. The confirm message dialog box is used here.
The confirm message dialog box is usually used to allow users to make choices, such as: "Are you right?" and so on. A dialog box pops up (including an OK button and a Cancel button).
Syntax: confirm(str);
Parameter description: str: The text to be displayed in the message dialog box returns: Boolean value
Return value:
Return true when the user clicks the "OK" button
Return to false when the user clicks the "Cancel" button
Note: The return value can be used to determine what button the user clicks.
<script type="text/javascript"> var mymessage=confirm("Do you like JavaScript?"); if(mymessage==true){ document.write("Good, come on!"); }else{ document.write("JS is powerful, you need to learn!"); }</script>Ask a question (prompt message dialog)
Sometimes, not only do you want the user to answer Yes/No. Instead, it is hoped for a more specific response. In this case, we can use the propt.
prompt pop-up message dialog box, usually used to ask for information that needs to interact with the user. A message dialog box pops up (including an OK button, a Cancel button and a text input box).
grammar:
prompt(str1,str2);
Parameter description:
str1: The text to be displayed in the message dialog box cannot be modified
str2: The content in the text box can be modified
Return value:
1. Click the OK button, and the content in the text box will return the value as a function.
2. Click the Cancel button to return to null
function rec(){ var score; //score variable is used to store the score value entered by the user. score = prompt("Please enter your score","90"); if(score>=90){ document.write("You are great!"); }else if(score>=75){ document.write("Good!"); }else if(score>=60){ document.write("Come on!"); }else{ document.write("Work hard!"); };} ; <script> var myName = prompt("Enter your name"); if(myName != null && myName != ""){ document.write("welcom to" + myName); }else{ document.write("welcom to" + myName); }</script>Open a new window (window.open)
grammar:
window.open([URL], [Window Name], [Parameter String])
Parameter description:
URL: Optional parameter, the URL or path of the web page to be displayed in the window. If this parameter is omitted, or its value is an empty string, the window will not display any documents.
Window name: Optional parameter, name of the opened window.
1. The name consists of letters, numbers and underscore characters.
2. Window name: Optional, this string is a comma-separated feature list that declares the name of the opened window. Can
It is "_top", "_blank", "_self", "_parent" , etc.
_blank displays the landing page in a new window
_self displays the destination web page in the current window
_parent frame web page displays the destination web page at the current entire window position
_top frame web page displays the destination web page in the upper window
3. Only one window with the same name can be created, and if you want to create multiple windows, the name cannot be the same.
4. The name cannot contain spaces.
Parameter string: Optional parameters, set window parameters, and separate each parameter with commas.
Parameter table:
t op Number Number Number The number of pixels at the top of the window leaving the top of the screen
left Number The number of pixels on the left end of the window leaving the left end of the screen
width Number The width of the window
height Number window height
menubar yes,no Is there a menu in the window
toolbar yes,no Is there a toolbar in the window
scrollbars yes,no Is there any scrollbar in the window
status yes,no Is there a status bar in the window
<script type="text/javascript"> window.open('http://','_blank','width=300,height=200,menubar=no,toolbar=no,status=no,scrollbars=yes') </script>Close the window (window.close)
close() close() close window
usage:
1 window.close();//Close this window 2 <Window object>.close();//Close the specified window
For example: Close the newly created window.
1 <script type="text/javascript">2 var mywin=window.open('//www.VeVB.COM'); //Storage the newly-printed window object in the variable mywin3 mywin.close();4 </script>innerHTML attribute
The innerHTML attribute is used to get or replace the content of an HTML element.
grammar:
Object.innerHTML
Object is the element object obtained, such as obtaining the element through document.getElementById("ID").
<h2 id="con">javascript</H2>
<script type="text/javascript"> var mychar=document.getElementById("con"); document.write("original title:"+mychar.innerHTML+"<br>"); //Output the original h2 tag content mychar.innerHTML="hello world" document.write("Modified title:"+mychar.innerHTML); //Output the modified h2 tag content</script>Change HTML style
grammar:
Object.style.property=new style;
Note: Object is the element object obtained , such as the element obtained through document.getElementById("id")
<h2 id="con">I love JavaScript</h2>
<script type="text/javascript"> var mychar= document.getElementById("con"); mychar.style.color="red"; mychar.style.background="#ccc"; mychar.style.width="300px";</script>Show and hide (display attribute)
grammar:
Object.style.display = value
Value:
none This element will not be displayed (and hidden)
block This element will be displayed as a block-level element (i.e., display)
mychar.style.display = "block"
Control class name (className attribute)
className property sets or returns the class property of the element.
grammar:
object.className = classname
effect:
1. Get the class attribute of the element
2. Assign a css style to an element in the web page to change the appearance of the element
p2.className = "two";
The above Javascript basic study notes (must-read for rookies) are all the content shared by the editor. I hope it can give you a reference and I hope you can support Wulin.com more.