计算两个数的和,js和html在同一目录下
// 文件名:add.js
function add(a, b) {
return a+b;
}
<head>
<script type="text/javascript" src="add.js"></script>
</head>
<body>
<input type="button" name="bt" value="计算和" onclick="alert(add(100, 200))">
</body>
script src引入的方式
-
本地引入:相对引入
-
远程引入:从远程服务器引入
在远程服务器上执行:
php -S 0.0.0.0:8090
快速简单的启用服务器Tab
,在页面上自动移动焦点。<!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 type="text/javascript" src="2.1.js"></script> </head> <body> <input type="button" name="bt" value="onfocus" onfocus="javascript:alert(123)"> <a href="javascript:alert(123)">欢迎</a> </body> </html>
window
当前浏览器的窗口ducument
当前文档<body> <script>window.alert('Windows表示当前的浏览器窗口,.表示在当前使用的对象中调用的方法')</script> <!-- .的含义:表示在当前使用的对象中调用方法 --> </body>
<script>document.write("这是中文,需要UTF-8编码。")</script> <br> <script>document.write("document.write,在当前文档中去写")</script>
<p id="demo" onclick="cg()">这是p标签</p> <script> function cg() { document.getElementById('demo').innerHTML="document.getElementById('demo')选择指定的对象,innerHTML将对应的内容修改了" } </script>
<!-- 在开发者工具的Console终端 --> <script> console.log("Catch me if you can.") </script>
综合练习
<!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 type="text/javascript" src="2.1.js"></script> </head> <body> <input type="button" value="onfocus me" onfocus="javascript:alert('按Tab键可以在页面上移动焦点')"> <script>window.alert("Window表示当前的浏览器窗口")</script> <p id