1.內行:在html代碼中添加
1 <a href="https://www.baidu.com" onclick="alert('您將要跳轉到百度')">百度</a>
注:不推薦使用這種方法,因為會導致可讀性太差
2.內嵌:在head標簽中添加
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title></title> 6 <script> 7 window.onload = function () { 8 alert('你好') 9 } 10 </script> 11 </head> 12 <body> 13 </body> 14 </html>
注:一點要添加<script></script>標簽,就跟css差不多,同時也可以在body標簽里添加,這一種方法適合代碼量較少的情況
3.外鏈:在head標簽中寫一個鏈接,類似css
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title></title> 6 <script src="one.js"></script> 7 </head> 8 <body> 9 </body> 10 </html>
注:最常用的編寫css方法,幾乎用在要寫大量js代碼的情況下
