1.使用<script>标签,如:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>JS</title>
</head>
<body>
<script>
var r=3;
function S1(r){
return r*r*3.14;
}
document.write("半径为3的圆面积为:"+S1(r));
</script>
</body>
</html>
2.外部javascript,从外部导入js,如:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>JS</title>
</head>
<body>
<script type="text/javascript" src="S1.js"></script>
</body>
</html>
JS代码如下:
// JavaScript Document
var r=3;
function S1(r){
return r*r*3.14;
}
document.write("半径为3的圆面积为:"+S1(r));
上面两例显示效果为:
3.内联javascript,将js代码写入HTML代码中,如:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>JS</title>
</head>
<body>
<button onClick="javacript:alert('我被点击了')">点击</button>
</body>
</html>
显示效果为: