1,
2,简单案例:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
-
- <script>
- alert("hello world") // 弹窗
-
- // document.write("hello world") // 写入页面
-
- console.log("hello world") // 写入控制台
- </script>
- </head>
- <body>
- <div>hello 11111</div>
- </body>
- </html>
复制代码 3,js放的位置
3.1 行内:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <button onclick="alert('hello world')">点我</button>
- <a href="javascript:alert('hello world')">点我</a>
- </body>
- </html>
复制代码 3.2 内嵌:
- <!DOCTYPE html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <div>hello 11111</div>
-
- <script>
- alert("hello world")
- // document.write("hello world")
- console.log("hello world")
- </script>
- </body>
- </html>
复制代码 3.3 外部:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <script src="js/hello.js">
- </script>
- </head>
- <body>
- </body>
- </html>
复制代码 hello.js
复制代码
|