Home  >  Article  >  Web Front-end  >  How to use javascript code in html

How to use javascript code in html

PHPz
PHPzOriginal
2023-04-24 10:53:382314browse

HTML is the basic language for web design. Using JavaScript in web pages can enhance its interactivity, dynamics and practicality. This article will provide an in-depth introduction to how HTML uses JavaScript code.

1. Introduction of javascript code

Using JavaScript code in HTML pages requires a medium-script tag. Generally, script tags are located in the head or body part of the HTML file. The following is a simple example:

<!DOCTYPE html>
<html>
   <head>
      <title>使用JavaScript代码</title>
      <script type="text/javascript">
          //JavaScript代码在这里
      </script>
   </head>
   <body>
      <!-- HTML页面中的其他内容 -->
   </body>
</html>

In the above code, we use the script tag to introduce JavaScript code in the head tag and write some JavaScript code in it. In actual development, we may write JavaScript code in a separate .js file and then introduce it through script tags.

<!DOCTYPE html>
<html>
   <head>
      <title>使用JavaScript代码</title>
      <script type="text/javascript" src="myscript.js"></script>
   </head>
   <body>
      <!-- HTML页面中的其他内容 -->
   </body>
</html>

In the above code, we introduced a file named myscript.js through the src attribute, which contains JavaScript code.

2. The location of JavaScript code

JavaScript code can be placed in various parts of the HTML file, such as the head, body, and various tags of the HTML page. Placing JavaScript code in different locations will affect the execution time and execution scope of the code.

  1. Placed in the head tag

The JavaScript code placed in the head tag is usually used to initialize global variables, define objects and functions. Since the code in the head tag will be executed immediately after the page is loaded, some preprocessing functions can be provided for the page before the page is rendered.

<!DOCTYPE html>
<html>
   <head>
      <title>使用JavaScript代码</title>
      <script type="text/javascript">
         var myVar = "Hello, World!";
         
         function myFunction() {
            alert(myVar);
         }
      </script>
   </head>
   <body>
      <!-- HTML页面中的其他内容 -->
   </body>
</html>

In the above code, we defined a variable named myVar and a function named myFunction. These codes will be executed immediately after the page is loaded and can provide some pre-processing functions for the page before rendering.

  1. Placed in the body tag

The JavaScript code placed in the body tag is usually used to handle page interaction and event handling. Since the code in the body tag will be executed after the page is loaded, it can provide more interactivity and dynamics to the page after the page is rendered.

<!DOCTYPE html>
<html>
   <head>
      <title>使用JavaScript代码</title>
   </head>
   <body>
      <h1>我的网页</h1>
      <button onclick="myFunction()">点击这里</button>
      <script type="text/javascript">
          function myFunction() {
            alert("你点击了这个按钮!");
         }
      </script>
   </body>
</html>

In the above code, we define a button and a JavaScript function named myFunction in the body tag. When the user clicks the button, the myFunction function will be triggered and a prompt box will pop up.

  1. Place within various tags

JavaScript code placed within various HTML tags is usually used to dynamically update content and styles based on user interaction. For example, we can write JavaScript code directly in the onclick or onmouseover attribute of a certain tag, so that when the user interacts with the tag, the content or style of the page will be changed.

<!DOCTYPE html>
<html>
   <head>
      <title>使用JavaScript代码</title>
      <style>
         .myDiv {
            background-color: yellow;
         }
      </style>
   </head>
   <body>
      <h1>我的网页</h1>
      <div class="myDiv" onmouseover="this.style.backgroundColor=&#39;red&#39;"
         onmouseout="this.style.backgroundColor=&#39;yellow&#39;">
         <p>这个div会改变颜色!</p>
      </div>
   </body>
</html>

In the above code, we define a div tag named myDiv and add JavaScript code to its onmouseover and onmouseout attributes. When the user moves the mouse over myDiv, the div's background color will change to red; when the user moves the mouse away, the div's background color will change back to yellow.

3. Use of JavaScript code

When using JavaScript code in HTML pages, you need to consider the following aspects:

  1. Variables and data types

In JavaScript, variables can store data, and variable names are case-sensitive. JavaScript supports a variety of data types, including numbers, strings, Boolean values, arrays, objects, and more.

<!DOCTYPE html>
<html>
   <head>
      <title>使用JavaScript代码</title>
   </head>
   <body>
      <h1>我的网页</h1>
      <script type="text/javascript">
         var myNum = 123;
         var myStr = "Hello, World!";
         var myBool = true;
         var myArr = [1,2,3,4,5];
         var myObj = {name:"John", age:30, city:"New York"};

         document.write(myNum); // 输出 123
         document.write("<br>");
         document.write(myStr); // 输出 Hello, World!
         document.write("<br>");
         document.write(myBool); // 输出 true
         document.write("<br>");
         document.write(myArr[0]); // 输出 1
         document.write("<br>");
         document.write(myObj.name); // 输出 John
         document.write("<br>");
      </script>
   </body>
</html>

In the above code, we define 5 different variables and use the document.write() function to output their values ​​on the page. We also demonstrated how to access elements in arrays and objects.

  1. Functions and events

A function in JavaScript is a piece of reusable code that can be called and executed through the function name. Functions can receive parameters and can have return values. In HTML pages, functions are usually used to handle user events, validate input, etc.

<!DOCTYPE html>
<html>
   <head>
      <title>使用JavaScript代码</title>
   </head>
   <body>
      <h1>我的网页</h1>
      <button onclick="myFunction()">点击这里</button>
      <p id="demo"></p>
      <script type="text/javascript">
          function myFunction() {
            document.getElementById("demo").innerHTML = "你点击了这个按钮!";
         }
      </script>
   </body>
</html>

In the above code, we define a function named myFunction and bind it to the onclick event of a button. When the user clicks the button, the myFunction function will modify the innerHTML attribute of the p tag and set the text content to "You clicked this button!"

  1. DOM operation

DOM (Document Object Model) is a programming interface for HTML documents, which allows JavaScript code to access any element in the document and modify its content, attributes, styles, etc. In HTML pages, DOM operations are usually used to dynamically change page content and style.

<!DOCTYPE html>
<html>
   <head>
      <title>使用JavaScript代码</title>
      <style>
         .myDiv {
            background-color: yellow;
         }
      </style>
   </head>
   <body>
      <h1>我的网页</h1>
      <div id="myDiv">
         <p>这里是一个div。</p>
      </div>
      <button onclick="myFunction()">点击这里</button>
      <script type="text/javascript">
          function myFunction() {
            var myDiv = document.getElementById("myDiv");
            myDiv.style.backgroundColor = "red";
         }
      </script>
   </body>
</html>

In the above code, we define a div tag named myDiv and a function named myFunction. When the user clicks the button, the myFunction function will modify the background color of the myDiv label through DOM operations.

4. Summary

Using JavaScript code in HTML pages can enhance the interactivity, dynamics and practicality of the page. This article introduces how to use script tags to introduce JavaScript code, where to place JavaScript code in HTML pages, JavaScript variables, data types, functions and events, and DOM operations. By in-depth learning and mastering these knowledge points, we can better use JavaScript to add more functions and beauty to our web pages.

The above is the detailed content of How to use javascript code in html. For more information, please follow other related articles on the PHP Chinese website!

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