一. 输出内容
js可以在以下位置直接输出内容
-
警告框:alert(“xxxx”);
-
控制台:console.log(“xxx”);
-
浏览器:document.write(“xxx”);
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>测试</title> <script src="js/jia.js"></script> <script type="text/javascript"> alert("警告框"); console.log("控制台"); document.write("<h1>浏览器内容区</h1>") </script> </head> <body> </body> </html>
二. js的编写位置
1. script标签内
<script type="text/javascript"> alert("警告框"); </script>
2. 单独的js文件
<script src="js/jia.js"></script>
3. html标签内
将js写在html标签内又分为以下几种:
-
a标签的href属性
-
其他标签的事件属性
-
阻止超链接的默认行为
下面针对上面的几种情况分别举例:a标签的href属性:
<a href="javascript:alert('警告框')" ></a> <a href='javascript:alert("警告框")'></a>
注意,如果双引号里面有字符串,那么字符串需要用单引号,如果单引号里面有字符串,那么字符串需要用双引号
其他标签的事件属性:
<button onclick="alert('点击了按钮')">按钮</button> <input type="text" onfocus="console.log('得到了焦点')" onblur="console.log('失去了焦点')">
阻止超链接的默认行为:
<a href="javascript:void(0)"> 阻止链接的默认行为</a> <a href="javascript:;"> 阻止链接的默认行为简写版</a>
请登录之后再进行评论