JavaScript study notes (a) - Usage

Author: This article is a blogger LI Qin original article, shall not be reproduced without the bloggers allowed.
This link: https://blog.csdn.net/weixin_42320142/article/details/102643333

Foreword

Recently the company projects to use Vue.js, I will review the js, write study notes.


HTML scripts must be located between the <script> and </ script> tag.

The script may be placed in an HTML page <body> and <head> section.


<Script> tag

To insert JavaScript in the HTML page, use the <script> tag.

<Script> and </ script> JavaScript will tell where to begin and end.

The lines between the <script> and </ script> contains JavaScript

<script>
	alert("我的第一个 JavaScript");
</script>

<Body> in JavaScript

JavaScript will write the text to the HTML <body> when the page loads:

<html>
<body>
.
.
<script>
	document.write("<h1>这是一个标题</h1>");
	document.write("<p>这是一个段落</p>");
</script>
.
.
</body>
</html>

<Head> JavaScript functions

Function is called when the button is clicked:

<!DOCTYPE html>
<html>
<head>
<script>
function myFunction()
{
    document.getElementById("demo").innerHTML="我的第一个 JavaScript 函数";
}
</script>
</head>
<body>
<h1>我的 Web 页面</h1>
<p id="demo">一个段落</p>
<button type="button" onclick="myFunction()">尝试一下</button>
</body>
</html>

<Body> JavaScript functions

Function is called when the button is clicked:

<!DOCTYPE html>
<html>
<body>
<h1>我的 Web 页面</h1>
<p id="demo">一个段落</p>
<button type="button" onclick="myFunction()">尝试一下</button>
<script>
function myFunction()
{
    document.getElementById("demo").innerHTML="我的第一个 JavaScript 函数";
}
</script>
</body>
</html>

External JavaScript

External code file typically contains a plurality of pages used.
File extension is external JavaScript file .js.
To use an external file, provided the .js file "src" attribute <script> tag:

<!DOCTYPE html>
<html>
<body>
<script src="myScript.js"></script>
</body>
</html>

Guess you like

Origin blog.csdn.net/weixin_42320142/article/details/102643333