一、行内式
1.代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="button" value="唐伯虎" onclick="alert('hi,秋香')"></input>
</body>
</html>
2.演示效果
二、内嵌式
1.代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
alert('你好啊,同学');
</script>
</head>
<body>
<!-- <input type="button" value="唐伯虎" οnclick="alert('hi,秋香')"></input> -->
</body>
</html>
2.演示效果
三、外部式
1.代码
注意:我们如果从外部引入script代码,那么不能在这个引入的script标签的内部再书写js代码了!不可以!
html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../js/alert.js"></script>
</head>
<body>
</body>
</html>
js代码:
alert('如果我们重新来过,你会不会爱我')