一、行内式
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('王五')"></input> </body> </html>
二、内嵌式
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>
三、外部式
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> <script src="../js/alert.js"></script> </html>
alert('你好世界')