<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>在页面上显示一个Helloworld</title>
<script type="text/javascript">
//实现页面加载helloworld
document.write('helloworld');
</script>
</head><body></body></html>
该代码就会显示helloworld
js有三种使用方式,分别为行内js,内部结js和外部js
行内js:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js使用方式1.行内js</title>
<script type="text/javascript">
</script>
</head>
<body>
<input type="button" value="点击有惊喜" onclick="javascript:alert('哈哈哈')"/>
</body>
</html>
内部js:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js使用方式2.内部js</title>
<script type="text/javascript">
function surprise(){
alert('恭喜你中了100万')
}
</script>
</head>
<body>
<input type="button" value="点击有惊喜" onclick="surprise()"/>
<input type="button" value="点击" onclick="surprise()"/>
<input type="button" value="双击" ondblclick="surprise()"/>
</body>
</html>4
外部js:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js使用方式3.外部js</title>
<script src="js/1.js" language="JavaScript"></script>
</head>
<body>
<input type="button" value="点击" onclick="test()"/>
</body>
</html>
在外部建立一个相关的js文件:
function test(){
alert('你中了100万')
}