一、hello world
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<!--JS代码需要编写到script标签中-->
<script type="text/javascript">
/*
* 控制浏览器弹出一个警告框
* alert("hello world");
*/
/*
* 让计算机在页面中输出内容
* document.write()可以向body中输出一个内容
* document.write("在页面中输出内容~~~");
*/
/*
* 向控制台输出内容
* console.log()的作用是向控制台输出内容
* console.log("向控制台输出内容");
*/
alert("hello world");
document.write("在页面中输出内容~~~");
console.log("向控制台输出内容");
</script>
</head>
<body>
</body>
</html>
二、js编写的位置
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<!--
可以将js代码编写到外部js文件中,然后通过script标签引入
写到外部文件中可以在不同的页面中同时引用,也可以利用到浏览器的缓存机制
推荐使用的方式
-->
<!--
script标签一旦用于引入外部文件了,就不能在编写代码了,即使编写了浏览器也会忽略
如果需要则可以在创建一个新的script标签用于编写内部代码
-->
<script type="text/javascript" src="js/script.js"></script>
<script type="text/javascript">
alert("我是内部的JS代码");
</script>
<!--
可以将js代码编写到script标签
-->
<script type="text/javascript">
alert("我是script标签中的代码!!");
</script>
</head>
<body>
<!--
可以将js代码编写到标签的onclick属性中
当点击按钮时,js代码才会执行
虽然可以写在标签的属性中,但是它们属于结构与行为耦合,不方便维护,不推荐使用
-->
<button onclick="alert('按钮弹框');">我是一个按钮</button>
<!--
可以将js代码写在超链接的href属性中,这样当点击超链接时,会执行js代码
-->
<a href="javascript:alert('我是一个超链接警告!!!');">超链接</a>
<a href="javascript:;">没有警告的超链接</a>
</body>
</html>
js/script
alert('我是外部JS');