一、外部JavaScript文件,不需要寫<SCRIPT>標簽,myScript.js代碼如下:
二、index.php代碼如下
三、運行效果如下
四、點擊之后
myScript.php如下
function myFunction() { document.getElementById("demo").innerHTML = "段落被更改。"; }
index.php如下
<!DOCTYPE html> <html> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <body> <h2>外部 JavaScript</h2> <p id="demo">一個段落。</p> <button type="button" onclick="myFunction()">試一試</button> <script src="myScript.js"></script> </body> </html>