Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of basic usage of JavaScript

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "case analysis of JavaScript basic usage". In daily operation, I believe many people have doubts about JavaScript basic use case analysis. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "JavaScript basic use case analysis". Next, please follow the editor to study!

Introduction to the basic usage of JavaScript

The script in HTML must be between the and tag.

Scripts can be placed in the and sections of the HTML page.

Label

To insert JavaScript in a HTML page, use a tag.

And will tell JavaScript where to start and end.

The line of code between and contains _ JavaScript:

Alert ("my first JavaScript")

Give it a try

You do not need to understand the above code. Just understand that the browser interprets and executes the JavaScript code between and

Old instances of lamp may use type= "text/javascript" in the tag. It is no longer necessary to do so. JavaScript is the default scripting language in all modern browsers as well as in HTML5.

JavaScript in

In this example, JavaScript writes text to HTML when the page loads:

Example

.

.

[xss_clean] ("this is a title")

[xss_clean] ("

This is a paragraph.

")

.

.

JavaScript functions and events

The JavaScript statement in the above example is executed when the page is loaded.

Typically, we need to execute code when an event occurs, such as when the user clicks a button.

If we put the JavaScript code into the function, we can call the function when the event occurs.

You will learn more about JavaScript functions and events in later chapters.

In or in the JavaScript

You can put an unlimited number of scripts in the HTML document.

The script can be in or part of a HTML, or in both parts.

The usual practice is to put the function in the section, or at the bottom of the page. This allows you to put them in the same place without interfering with the content of the page.

JavaScript function in

In this example, we put a JavaScript function in the section of the HTML page.

This function is called when the button is clicked:

Example

Function myFunction ()

{

Document.getElementById ("demo"). InnerHTML= "my first JavaScript function"

}

My Web page

A paragraph

Give it a try

Function myFunction ()

{

Document.getElementById ("demo"). InnerHTML= "my first JavaScript function"

}

Tip: we put JavaScript at the bottom of the page code to make sure that in the

The script is executed after the element is created.

External JavaScript

You can also save the script to an external file. External files usually contain code used by multiple web pages.

The file extension of the external JavaScript file is .js.

To use an external file, set the .js file in the "src" attribute of the tag:

Example

You can put the script in or in the actual running effect is exactly the same as you write the script in the tag.

The myScript.js file code is as follows:

Function myFunction () {

Document.getElementById ("demo"). InnerHTML= "my first JavaScript function"

}

At this point, the study on "case Analysis of the basic usage of JavaScript" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report