Represents the type of text between <script></script> Text,javascript is to tell the browser that the text inside is a JavaScript language
We can place JavaScript code anywhere in the HTML file, but we usually place it in the head or body of the page.
Put in the
The most common way is to place the <script> element in the head section of the page, and the browser parses the head section to execute the code before parsing the rest of the page.
Put in the <body> section
The JavaScript code executes when the page reads to the statement.
Recognize statements and symbols
JavaScript statements are commands that are sent to the browser. The purpose of these commands is to tell the browser what to do.
Each line of JavaScript code format: 语句;
Let's take a look at the following code
<script type= "Text/javascript" > alert ("hello!"); </script>
In the example alert("hello!"); is a JavaScript statement.
The end of a line is considered the end of the statement, usually with a semicolon at the end ";" to represent the end of the statement.
Look at this code, there are three statements, each sentence after the end of ";", in order to execute the statement.
<script type= "Text/javascript" > document.write ("I"); document.write ("Love"); document.write ("JavaScript");</script>
Attention:
1. ";" Semicolon to enter in the English state, the same, JS code and symbols are entered in the English state.
2. Although the semicolon ";" can also not write, but we have to develop a good habit of programming, remember to write a semicolon at the end of the statement.
Note is important
The role of annotations is to improve the readability of your code, to help you and others to read and understand the JavaScript code you write, and the content of the comments will not be displayed on the page. Comments can be divided into single-line comments and multiline comments.
For readability, the comment is generally placed at or around the end of the statement that needs to be interpreted.
A single-line comment, with the symbol "//" before the content of the comment.
<script type= "Text/javascript" > document.write ("single-line annotation using '//'"); I am the comment that the statement function outputs content in a Web page </script>
Multiline comments start with "/*" and End with "*/".
<script type= "Text/javascript" > document.write ("Multi-line Comment use/* Comment content */"); /* Multiline comment develop good habit of writing notes */</script>
What is a variable
What is a variable? Literally, a variable is a variable amount; From a programmatic standpoint, a variable is a memory used to store a certain number of values. We can think of variables as a box, in order to distinguish the box, you can use Box1,box2 and other names to represent different boxes, BOX1 is the name of the box (that is, the name of the variable).
Define variables using the keyword VAR, with the following syntax:
var variable name
Variable names can be named arbitrarily, but follow the naming conventions:
1. The variable must start with a letter, an underscore (_), or a dollar ($) symbol.
2. You can then use any number of English letters, numbers, underscores (_), or dollar ($) characters.
3. You cannot use JavaScript keywords with javascript reserved words.
Variables must be declared and then assigned, as follows:
var mychar;mychar= "JavaScript"; var mynum = 6;
Variables can be assigned repeatedly, as follows:
var mychar;mychar= "javascript"; mychar= "Hello";
Attention:
1. In JS, case-sensitive, such as variable MyChar and MyChar is not the same, the expression is two variables.
2. Variables Although can also not be declared, directly used, but not standardized, need to first declare, after use.
Judgment Statement (If...else)
The If...else statement executes the code when the specified condition is true and executes the else code when the condition is not established.
Grammar :
if Conditions {Code executed when condition is set} Else {Code executed when the condition is not valid}
Let's say we age to determine whether an adult, such as the age of 18 years old, is an adult, or not an adult. The code represents the following :
<script type= "Text/javascript" > var myage =; if (myage>=18) //myage>=18 is the judging condition {document.write ("You are an adult. ");} else //otherwise age less than {document.write ("Under 18, you are not an adult.") ");} </script>
What is a function
A function is a set of statements that complete a particular function. Without a function, it may take five lines, 10 rows, or even more code to complete the task. In this case, we can put the code block that completes the specific function into a function, call this function directly, save the trouble of repeatedly inputting a lot of code.
How do you define a function? The basic syntax is as follows:
function name () {function code;}
Description
1. function defines the keyword for the functions.
2. "Function name" you take the name of the function.
3. "Function code" is replaced with code that completes a specific function.
Let's write a simple function that implements the addition of two numbers and give the function a meaningful name: "ADD2", the code is as follows:
function Add2 () { var sum = 3 + 2; alert (sum);}
Function call:
Once the function is well defined, it cannot be executed automatically, so it needs to be called, just write the function directly where it is needed, and the code is as follows:
Output content (document.write): can be used to directly write content to the HTML output, simply to directly in the Web page output content
Output content (document.write)
document.write()Can be used to write content directly to the HTML output stream. The simple thing is to print the content directly in the Web page.
First Kind : output content is enclosed in "", Direct output "" content within the number.
<script type= "Text/javascript" > document.write ("I Love javascript! "); Content is output directly from the content in "", "". </script>
The second Kind : through variables, output content
<script type= "Text/javascript" > var mystr= "Hello world!"; document.write (mystr); Write the variable name directly, and the output variable stores the contents. </script>
Third Kind : output multiple content, between content + number of connections.
<script type= "Text/javascript" > var mystr= "Hello"; document.write (mystr+ "I love JavaScript"); Connect between multiple items with a + sign </script>
Fourth Type : Output HTML label, and function, label using "".
<script type= "Text/javascript" > var mystr= "Hello";d ocument.write (mystr+ "<br>");//Output Hello, Outputs a line break document.write ("JavaScript");</script>
Warning (Alert message dialog box)
When we visit the site, sometimes suddenly pop up a small window, which is written with a text message. If you do not click "OK", you can not do anything to the Web page, this small window is the use of alert implementation.
Grammar:
Alert (string or variable);
Look at the following code:
<script type= "Text/javascript" > var mynum =; Alert ("hello!"); Alert (mynum);</script>
Note:alert pops up the message dialog box (contains a OK button).
Result: Pop-up message box sequentially
Attention:
1. You cannot do anything else until you click the "OK" button in the dialog box.
2. Message dialogs are commonly used for debugging programs.
3. Alert output can be a string or variable, similar to document.write.
Confirmation (Confirm message dialog box)
The Confirm Message dialog box is often used to allow the user to make a selection action, such as: "Are you right?" such as A popup dialog box (including a OK button and a Cancel button).
Grammar:
Confirm (str);
Parameter description:
STR: The text to display in the message dialog box Boolean value
return value:
Returns True when the user taps the "OK" button, which returns False when the user taps the "Cancel" button
Note: The return value can determine what button the user clicked.
Look at the following code:
<script type= "Text/javascript" > var mymessage=confirm ("Do you like JavaScript?"); if (mymessage==true) { document.write ("Very good, come on!"); } else { document.write ("JS powerful, to learn Oh!"); } </script>
Results:
Note: The message dialog box is exclusive, that is, the user cannot do anything else until the dialog button is clicked.
Ask a question (Prompt message dialog box)
promptA pop-up message dialog box is typically used to ask for information that needs to interact with the user. Pop-up message dialog box (contains a OK button, Cancel button and a text input box).
Grammar:
Prompt (str1, str2);
Parameter description:
STR1: The text to display in the message dialog box cannot be modified STR2: The contents of the text box can be modified
return value:
return value:
1. Click the OK button and the contents of the text box will return the value 2 as the function. Clicking the Cancel button will return null
Take a look at the following code:
var myname=prompt ("Please enter your name:"), if (myname!=null) { alert ("Hello" +myname);} else { alert ("Hello my friend."); }
Results:
Note: You cannot perform any other action until the user taps the button in the dialog box.
Open a new window (window.open)
The open () method can look for an existing or new browser window.
Grammar:
window.open ([URL], [window name], [parameter string])
Parameter description:
URL: optional parameter, in the window to display the Web page's URL or path. If this argument is omitted, or if its value is an empty string, then the window will not display any documents. window name: optional parameter, the name of the window being opened. 1. The name consists of letters, numbers, and underscore characters. 2. " _top "," _blank "," _selft "have a special meaning name. _blank: Display the target page in a new window _self: Display the target page in the Current window _top: On the frames page, in the upper window, the target page 3. Windows of the same name can only be created one, and if you want to create multiple windows, name cannot be the same. 4.name cannot contain spaces. parameter string: optional parameter, set window parameters, each parameter separated by commas.
Parameter table:
For example: Open http://www.imooc.com website, size 300px * 200px, no menu, no toolbar, no status bar, scroll bar window:
<script type= "Text/javascript" > window.open (' http://www.imooc.com ', ' _blank ', ' width=300,height=200,menubar= No,toolbar=no, Status=no,scrollbars=yes ') </script>
Note: The running results consider browser compatibility issues.
Close Window (window.close)
Close () Closes the window
Usage:
Window.close (); Close this window
Or
< Window object >.close (); Closes the specified window
For example, close the newly created window.
<script type= "Text/javascript" > var mywin=window.open (' http://www.imooc.com ');//The newly-hit window object, stored in the variable Mywin mywin.close ();</script>
Note: The code above opens a new window, closes the window, and does not see the window being opened.
Programming Exercise: Create New button, "New window opens website", click to open New window
Task
<script>
function Openwage () {var open=confirm ("OK to open Web page?") ") if (open==true) {var openweb=prompt (" OK is this URL? ") "," http://www.baidu.com "), if (openweb!=null) {window.open (webadress," _blank ", ' width=400,height=500,menubar=no, Toolbar=no ')} else{alert ("End");} }else{alert ("End");}} </script>
Understanding the DOM
The Document Object Model DOM defines a standard way to access and manipulate HTML documents. The DOM renders an HTML document as a tree structure with elements, attributes, and text (the node tree).
Let's take a look at the following code:
decomposing HTML code into DOM node hierarchy diagram:
An HTML document can say a collection of nodes, three common DOM nodes:
1. ELEMENT nodes:
2. Text node: content that is presented to the user, such as JavaScript, DOM, CSS, and so on in <li>...</li>.
3. Attribute node: element properties, such as the <a> tag's link property href= "http://www.imooc.com".
Look at the following code:
<a href= "http://www.imooc.com" >javascript dom</a>
Get elements by ID
Learn html/css style, all know that the page is organized by the label information, and the tag id attribute value is unique, as if each person has a social security number, as long as the ID number can be found corresponding to the person. So in the Web page, we find the tag first, and then we do the operation.
Grammar:
Take a look at the following code:
Result: null or [object Htmlparagraphelement]
Note: The obtained element is an object, such as the element that you want to manipulate, and we want to pass its properties or methods.
InnerHTML Property
The InnerHTML property is used to get or replace the contents of an HTML element.
Grammar:
Object.innerhtml
Attention:
1.Object is an element that gets an element object, such as a document.getElementById ("ID").
2. Note that writing, innerHTML is case-sensitive.
We get the <p> elements by id= "Con", and the contents of the elements are output and changed, the code is as follows:
Results:
Change HTML Style
HTML DOM allows JavaScript to change the style of HTML elements. How do you change the style of HTML elements?
Grammar:
Object.style.property=new style;
Note: object is an element that gets an element object, such as a document.getElementById ("id").
Basic attribute Table (property):
Note: This table is only a small subset of CSS style properties, and other styles can be set and modified by this method.
Take a look at the following code:
Change the style of the <p> element, change the color to red, change the font size to 20, change the background color to blue:
<p id= "Pcon" >hello world!</p><script> var mychar = document.getElementById ("Pcon"); MyChar. style.color= "Red"; MyChar. style.fontsize= "a"; MyChar. style.backgroundcolor = "Blue";</script>
Results:
Show and hide (Display properties)
The display and hidden effects are often seen in Web pages and can be set by the display property.
Grammar:
Object.style. Display = value
Note: object is an element that gets an element object, such as a document.getElementById ("id").
value takes values:
Take a look at the following code:
Control class name (ClassName property)
The ClassName property sets or returns the class property of the element.
Grammar:
Object.classname = ClassName
Role:
1. Get the class attribute of an element
2. Specify a CSS style for an element within the page to change the appearance of the element
Take a look at the following code to get the class attribute of the <p> element and change the classname:
The RemoveAttribute () method deletes the specified property.
Programming challenges
<!--When you click the appropriate button, do the appropriate action, add the corresponding event to the button--
<input type= "button" value= "Change Color" onclick= "ChangeColor ()" >
<input type= "button" value= "Change width height" onclick= "changesize ()" >
<input type= "button" value= "Hide Content" onclick= "Hidd ()" >
<input type= "button" value= "Show content" onclick= "show ()" >
<input type= "button" value= "Cancel Settings" onclick= "Calloff ()" >
</form>
<script type= "Text/javascript" >
var Change=document.getelementbyid ("txt");
Define a function that "changes color"
function ChangeColor () {
Change.style.color= "Red";
Change.style.backgroundcolor= "Pink";
}
Define a function that "changes the width of the height"
function Changesize () {
Change.style.width= "50px";
Change.style.height= "100px";
}
Functions that define "hidden content"
function Hidd () {
Change.style.display= "None";
}
Functions that define "show content"
Function Show () {
change.style.display= "Block";
}
Functions that define "Cancel settings"
function Calloff () {
var queding=confirm ("OK to reset?") ");
if (queding==true) {Change.removeattribute ("style")}
}
</script>
The second method of
defined in the same array
<input type= "button" value= "Change Color" onclick= "Set.changecolor ()" >
<input type= "button" value= "Change width height" onclick= "set.changesize ()" >
<input type= "button" value= "Hide Content" onclick= "Set.objhide ()" >
<input type= "button" value= "Show content" onclick= "Set.objshow ()" >
<input type= "button" value= "Cancel Settings" onclick= "Set.offset ()" >
</form>
<script type= "Text/javascript" >
var Txt=document.getelementbyid ("txt");
var set={
Changecolor:function () {
Txt.style.color= "Red";
Txt.style.backgroundcolor= "#ccc";
},
Changesize:function () {
Txt.style.width= "300px";
Txt.style.height= "300px";
},
Objhide:function () {
Txt.style.display= "None";
},
Objshow:function () {
txt.style.display= "Block";
},
Offset:function () {
var message=confirm ("Are you sure you want to reset all settings?") ");
if (message==true) {
Txt.removeattribute (' style ');
}
}
}
</script>
JS Basic Tutorial Learning Notes