Home >Web Front-end >Front-end Q&A >How to use javascript in web pages

How to use javascript in web pages

PHPz
PHPzOriginal
2023-04-24 10:52:461374browse

With the development of the Internet, the interactivity of web pages is becoming more and more important, and JavaScript is a very useful programming language. It can make web pages more interactive between users and web pages, thereby improving user experience. . So, how to use JavaScript in web pages? This article will introduce it from the following aspects.

1. Basic syntax of JavaScript

Before using JavaScript, we need to understand some of its basic syntax. Similar to other programming languages, JavaScript also has basic syntax such as variables, loops, and functions. For example:

//Define variable var name = "John"; //Define function sayHello(name) { console.log("Hello " name "!"); } //Loop for (var i = 0; i < 10; i ) { console.log(i); }

2. Embedding JavaScript in HTML files

When embedding JavaScript in HTML files, we need to use <script> tag. For example: </p> <p><!DOCTYPE html> <html> <head> <title>My Page</title> </head> <body> <h1>Hello World!< /h1> <script> console.log("Hello World!"); </script>

In this example, we are in The <script> tag is used in the tag, and then JavaScript code is written in it. When the browser renders the entire HTML file, the JavaScript code in it is automatically executed. </p> <p>3. Operating HTML in JavaScript</p> <p>JavaScript can help us operate HTML elements. For example, use the getElementById method to obtain the element with the specified ID and modify its content or attributes. For example: </p> <p><!DOCTYPE html> <html> <head> <title>My Page</title> </head> <body> <h1 id="myHeading"&gt ;Hello World!</h1> <button onclick="changeText()">Click me</button> <script> function changeText() { document.getElementById("myHeading").innerHTML = "Hello JavaScript !"; } </script>

In this example, we use getElementById to get the h1 element with the ID myHeading, and modify it when the button is clicked its innerHTML.

4. Use jQuery to quickly operate HTML

In addition to native JavaScript operating HTML, we can also use the third-party library jQuery to quickly operate HTML elements. jQuery is very popular because it simplifies code for DOM manipulation and provides convenient functions and methods. For example:

My Page

Hello World!

<script> $("#myButton").click(function(){ $("#myHeading").html("Hello jQuery!"); }); </script&gt ; </body> </html></p> <p>In this example, we introduced the jQuery library and used its click function and html method to modify the content of the h1 element by clicking the button. </p> <p>5. Handling user interaction</p> <p>One of the most useful functions of using JavaScript is handling user interaction. For example, we can monitor the user's clicks, mouse movements, key presses and other operations, and execute corresponding code based on these operations. For example: </p> <p><!DOCTYPE html> <html> <head> <title>My Page</title> <script src="https://code.jquery.com/jquery- 3.6.0.min.js"></script>

Hello World!