Script tag
label | description |
---|---|
script | It defines the client-side script |
noscript | Defines the text browser does not support script output |
Note :
- If you use the "src" attribute, the script element must be empty.
- If the internal elements of the script code is not located in one function, then the code will be executed immediately when the page is loaded
The sample code
- example1
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<p>
JavaScript 能够直接写入 HTML 输出流中:
</p>
<script>
document.write("<h1>这是一个标题</h1>");
document.write("<p>这是<strong>一个段</strong>落。</p>");
</script>
<p>
您只能在 HTML 输出流中使用 <strong>document.write</strong>。
如果您在文档已加载后使用它(比如在函数中),会覆盖整个文档。
</p>
</body>
</html>
- example2
<!DOCTYPE html>
<html>
<body>
<h1>My First JavaScript</h1>
<p id="demo">
JavaScript can react to events. Like the click of a button.
</p>
<script>
function myFunction()
{
document.getElementById("demo").innerHTML="Hello JavaScript!";
}
</script>
<button type="button" onclick="myFunction()">Click Me!</button>
</body>
</html>