search
HomeWeb Front-endJS TutorialSummary of knowledge points in the JavaScript introductory series_javascript skills

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

<script> tags should appear in pairs, and write JavaScript code between <script></script>.


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(, , )

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

.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, ,

,

, 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

  • ...
  • .

    3. Attribute node: element attribute, such as the link attribute href="http://www.imooc.com" of the 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
    Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

    The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

    Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

    Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

    Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

    Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

    JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

    JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

    JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

    JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

    Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

    Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

    JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

    The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

    The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

    Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Article

    Hot Tools

    EditPlus Chinese cracked version

    EditPlus Chinese cracked version

    Small size, syntax highlighting, does not support code prompt function

    SublimeText3 English version

    SublimeText3 English version

    Recommended: Win version, supports code prompts!

    PhpStorm Mac version

    PhpStorm Mac version

    The latest (2018.2.1) professional PHP integrated development tool

    Dreamweaver Mac version

    Dreamweaver Mac version

    Visual web development tools

    Safe Exam Browser

    Safe Exam Browser

    Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.