Home >Web Front-end >JS Tutorial >Summary of knowledge points in the JavaScript introductory series_javascript skills

Summary of knowledge points in the JavaScript introductory series_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:08:101073browse

JavaScript is a literal scripting language. It is a dynamically typed, weakly typed, prototype-based language with built-in support for types. Its interpreter is called the JavaScript engine, which is part of the browser and is widely used in client-side scripting languages. It was first used on HTML (an application under Standard Universal Markup Language) web pages to add dynamic functions to HTML web pages. .

1. Preparation:

1. Why learn JavaScript

All major browsers are Javascript only

Most web pages use Javascript

It can make web pages present various dynamic effects

Easy to learn

2. How to insert JS

3f1c4e4b6b16bbbd69b2ee476dc4f83a tags should appear in pairs, and write JavaScript code between 3f1c4e4b6b16bbbd69b2ee476dc4f83a2cacc6d41bbb37262a98f745aa00fbf0.

4ec11beb6c39d0703d1751d203c17053 means that the text type (text) between 3f1c4e4b6b16bbbd69b2ee476dc4f83a2cacc6d41bbb37262a98f745aa00fbf0 is used to tell the browser that the text inside belongs to the JavaScript language.

3. Reference JS external files

You can separate the HTML file and JS code and create a separate JavaScript file (JS file for short). The file suffix is ​​usually .js
In the JS file, there is no need for the 3f1c4e4b6b16bbbd69b2ee476dc4f83a tag, just write JavaScript code directly.

Copy code The code is as follows:

84cf5d7ad8199c88ca1d921cae010baf2cacc6d41bbb37262a98f745aa00fbf0

4. The position of JS in the page

As a scripting language, JavaScript can be placed anywhere in the HTML page. We usually place it in the head or body part of the web page. But the browser interprets HTML in order, so the previous script is executed first. For example, the js for page display initialization must be placed in the head, because the initialization must be done in advance (such as setting css for the page body, etc.); and if the function is executed through an event call, there is no requirement for the location.

5. Statements and symbols

The format of each JavaScript code: statement;

6. Annotation method

For single-line comments, add the symbol "//" before the comment content.

Multi-line comments start with "/*" and end with "*/".

7. Variables

Define variables using the keyword var, the syntax is as follows:

var variable name

The variable name can be named arbitrarily, but the naming rules must be followed:

1. Variable names must start with letters or underscores (_).

2. Variable names must use English letters, numbers, and underscores (_).

3. JavaScript keywords and JavaScript reserved words cannot be used in variable names.

8. Judgment statement (if...else)

Syntax:

if(条件)
{ 条件成立时执行的代码 }
else
{ 条件不成立时执行的代码 }

9. Function

function 函数名()
{
函数代码;
}

Description:

1. function keyword defines a function.

2. "Function name" is the name you give the function.

3. Replace "function code" with code that completes a specific function.

Function call:

After a function is defined, it cannot be executed automatically, so if you need to call it, just write the function directly at the required location

2. Commonly used interaction methods

1. Output content (document.write)

document.write() can be used to write content directly to the HTML output stream. To put it simply, it is to output the content directly on the web page.

The first type: the output content is enclosed in "", and the content within the "" is directly output.

The second type: output content through variables

The third type: output multiple contents, and connect the contents with a + sign.

The fourth method: output HTML tags and work. The tags are enclosed in "".

2. Warning (alert message dialog box)

A small window pops up with a message text (alert pops up a message dialog box (including an OK button)): alert (string or variable);

Note:

1. No other operations can be performed before clicking the "OK" button in the dialog box.

2. Message dialog boxes can usually be used to debug programs.

3. alert output content, which can be a string or variable, similar to document.write.

3. Confirm (confirm message dialog box)

Pop up a dialog box (including an OK button and a Cancel button). confirm(str);

Parameter description:

str: Text to be displayed in the message dialog box

Return value: Boolean value

Return value:

When the user clicks the "OK" button, return true

When the user clicks the "Cancel" button, return false

Note: The return value can be used to determine which button the user clicked

Note: The message dialog box is exclusive, that is, the user cannot perform any other operations before clicking the button in the dialog box.

4. Question (prompt message dialog box)

Pop up a message dialog box (including an OK button, a cancel button and a text input box) 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 be used as the function return value

2. Clicking the cancel button will return null

Note: No other operations can be performed before the user clicks the button in the dialog box.

5. Open a new window (window.open)

The open() method is used to open a new window: window.open(258c40d94d8689854ad79c4076dd5f96, a84845efeef0053bd27f708aedbdf5b7, 5e048d71ad0b12855ece671eadf2df13)

Parameter description:

URL: URL or path to open the window.

Window name: The name of the opened window.

Can be "_top", "_blank", "_selft", etc.

Parameter string: Set window parameters, each parameter is separated by commas. (top,left,width,height,menubar,toolbar,scrollbars,status)

Note:

1. There are spaces before and after commas and equal signs between parameters. The string is invalid. Only by removing the spaces can it run normally.

2. The running results consider browser compatibility issues.

6. Close the window (window.close)

window.close(); //Close the specified window

9bf54662ab0eebf19bebd7f4139e648f.close();

3. DOM operation

1. Get to know DOM

Document Object Model DOM (Document Object Model) defines standard methods for accessing and processing HTML documents. DOM renders HTML documents as elements,

Tree structure (node ​​tree) of attributes and text.

1. Element nodes: In the above picture, 100db36a723c770d327fc0aef2ce13b1, 6c04bd5ca3fcae76e30b72ad730ca86d, e388a4556c0f65e1904146cc1a846bee, etc. are all element nodes, that is, labels.

2. Text node: content displayed to the user, such as JavaScript, DOM, CSS and other texts in 25edfb22a4f469ecb59f1190150159c6...bed06894275b65c1ab86501b08a632eb.

3. Attribute node: element attribute, such as the link attribute href="http://www.imooc.com" of the 3499910bf9dac5ae3c52d5ede7383485 tag.

2. Get elements by ID

document.getElementById(“id”)

Note: The element obtained is an object. If we want to operate on the element, we have to use its properties or methods.

3. innerHTML attribute

The

.innerHTML property is used to get or replace the content of an HTML element.

Object.innerHTML

1.Object is the element object obtained, such as the element obtained through document.getElementById("ID").

2. Pay attention to writing, innerHTML is case sensitive

4. Change HTML style

Object.style.property=new style;

Note: Object is the element object obtained, such as the element obtained through document.getElementById("id").
Basic property table (property);

backgroundColor/height/width/color/font/fontFamily/fontSize

5. Show and hide (Display attribute)

Object.style.display = value

Note: Object is the element object obtained, such as the element obtained through document.getElementById("id").

value:

none: This element will not be displayed (hidden)

block: This element will be displayed as a block-level element (display)

6. Control class name (className attribute)

Set or return the class attribute of the element

object.className = classname

1. Get the class attribute of the element

2. Specify a css style for an element in the web page to change the appearance of the element

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn