問題描述:
通過script標簽外部引入javascript檔案,但不起作用,js代碼失效
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>登錄</title>
<script src="js/main.js" type="text/javascript"></script>
</head>
<body>
<form action="XXXXXX" method="post" name="login">
賬號:<input type="text" name="username" id="username" /> <br>
密碼:<input type="password" name="password" id="password" /> <br>
<input type="submit" name="login" id="login" value="登錄"/>
<input type="button" name="logon" id="logon" value="注冊" />
</form>
</body>
</html>
原因分析:
html代碼的執行順序是從上到下的,瀏覽器由上至下決議html代碼,如果在head里面引入js,可能會導致js執行時,頁面標簽還未加載,導致js找不到作用物件,從而失效
解決方案:
在body后面引入外部js檔案即可
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>登錄</title>
</head>
<body>
<form action="http://42.192.60.64:8080/user" method="post" name="login">
賬號:<input type="text" name="username" id="username" /> <br>
密碼:<input type="password" name="password" id="password" /> <br>
<input type="submit" name="login" id="login" value="登錄"/>
<input type="button" name="logon" id="logon" value="注冊" />
</form>
</body>
<script src="js/main.js" type="text/javascript"></script>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/290106.html
標籤:其他
