在此處輸入影像描述
我嘗試了一些解決方案來修復這個錯誤,比如 put<script> below </body>但它沒有用。那么我該如何解決這個問題。這是我的代碼。我想知道我的錯是什么,所以當我再次遇到這個錯誤時我可以解決這個問題。先謝謝
<body>
<div id="main">
<div id="login">
<h2><strong>Login</strong></h2>
<br>
<form id="info" ac>
<label>Username:</label>
<input type="text" placeholder="Input username" name="username" id="first">
<label>Password:</label>
<input type="password" placeholder="Input password" name="password" id="seconde">
<div>
<button id="bt">Login</button>
</div>
</form>
</div>
</div>
<script>
document.querySelector("bt").addEventListener("click",function(check){
var a = document.querySelector("#first").value;
var b = document.querySelector("#second").value;
if(a=="admin" && b=="123456"){
alert('Login Success!\nRedirecting to next page');
document.querySelector("#info").action="nextpage1.html"
}else{
alert('Login failed!\nIncorrect username or password');
}
})
</script>
</body>
uj5u.com熱心網友回復:
正如Phil 上面建議的那樣,document.querySelector將查找您在其中提到的任何內容。
既然你有bt它會尋找標簽<bt>。
因此,如果您想選擇一個 id ,請bt確保#在實際 id 的名稱之前使用。
或者如果你想選擇一個類,不要忘記.在實際類名之前使用。
例子:
用 ID 選擇 -
document.querySelector("#bt")用 ClassName 選擇 -
document.querySelector(".bt")
uj5u.com熱心網友回復:
顯示此錯誤是因為您必須指定您
在代碼中選擇什么(類/ID),您忘記在 id 名稱之前放置 #
document.querySelector("#bt")
你也可以使用
document.getElementById("bt")
uj5u.com熱心網友回復:
使用此代碼: 更新 1:在輸入中,將 id 從 seconde 更改為 second 更新 2:在 document.querySelector 中,添加 # 和 bt。
<body>
<div id="main">
<div id="login">
<h2><strong>Login</strong></h2>
<br>
<form id="info" ac>
<label>Username:</label>
<input type="text" placeholder="Input username" name="username" id="first">
<label>Password:</label>
<input type="password" placeholder="Input password" name="password" id="second">
<div>
<button id="bt">Login</button>
</div>
</form>
</div>
</div>
<script>
document.querySelector("#bt").addEventListener("click",function(check){
var a = document.querySelector("#first").value;
var b = document.querySelector("#second").value;
if(a=="admin" && b=="123456"){
alert('Login Success!\nRedirecting to next page');
document.querySelector("#info").action="nextpage1.html"
}else{
alert('Login failed!\nIncorrect username or password');
}
})
</script>
</body>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/457324.html
標籤:javascript html
