前置
js
javascript是一种完全的面向对象的有说
HTML中给的js必须包含在<script>
与</script>
之间
js代码脚本可以被放置在html中的<head>
和<body>
中
1 2 3
| <script> alert("my first script"); </script>
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| <!DOCTYPE html> <html> <body>
<script> document.write("<h1>这是一个标题</h1>"); document.write("<p>这是一个段落</p>"); </script>
</body> </html>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <!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>
|
也可以把js文件存在外部,保存为.js,但是调用的时候就是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h1>我的 Web 页面</h1> <p id="demo">一个段落。</p> <button type="button" onclick="myFunction()">点击这里</button> <p><b>注释:</b>myFunction 保存在名为 "myscript.js" 的外部文件中。</p> <script src="myscript.js"></script> </body> </html>
|
1 2 3 4
| function myFunction() { document.getElementById("demo").innerHTML="我的第一个 JavaScript 函数"; }
|
getelementById() 方法可返回对拥有指定 ID 的第一个对象的引用。
js中可以通过"\"
进行换行
但是你不能
1 2
| document.write \ ("你好世界!");
|
html中<p>
标签定义段落。