JavaScript: Use js script to write HTML code

Hello, everyone, my name is wangzirui32, today we will learn how to make js scripts automatically write HTML code.
First, open a js file, name it hello.js, and enter:

// 一个用于测试写入HTML代码的hello函数
// 用法:调用时在HTML页面上写入“Hello!”
function hello(){
    
    
	// document.write函数 用来写入括号中的HTML源码(为字符串)
	document.write("<h1>Hello!</h1>");
}
// 调用一次
hello();

Then, open an HTML file, put it in the same directory as hello.js, and enter:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>JavaScript 测试写入HTML</title>
</head>
<body>
  <script src="hello.js"></script>
</body>
</html>

Open it with a browser, and you can see the loaded "Hello!".

Guess you like

Origin blog.csdn.net/wangzirui32/article/details/113834486