在下面的代码中,click() 不起作用 我试过的问题是什么 $("login").on("click",function(){});功能也不是仍然不工作
<html>
<head>
<!--- Links --->
<link rel="stylesheet" href="bootstrap.min.css">
<link rel="stylesheet" href="style.css">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="main.js"></script>
</head>
<!--html-->
<title>Test Page</title>
<script>
$("#login").click(function() {
alert("hai");
});
</script>
</head>
<body onload="$('#reg').toggle();">
<div id="user-area">
<div class="btn-area">
<button id="btnlgn" class="btn btn-outline-primary" onclick="toggleReg()">Login /Register</button>
</div>
<div id="forms">
<div class="form" id="reg">
<form class="form-sigin" id="register">
<input type="text" class="form-control" placeholder="Email" id="email">
<input type="name" class="form-control" placeholder="Full Name" id="name">
<input type="password" class="form-control" placeholder="password" id="password">
<input type="password" class="form-control" placeholder="Re-enter password" id="conPassword">
<input type="tel" class="form-control" placeholder="Mobile Number" id="number">
</form>
<button class="btn" id="register">Register</button>
</div>
<div class="form" id="lgn">
<form class="form-sigin" id="sigin">
<input type="text" class="form-control" placeholder="Email" id="emaillgn">
<input type="password" class="form-control" placeholder="password" id="passwordlgn">
</form>
<button class="btn" id="login">Login</button>
</div>
</div>
</div>
</body>
</html>
我试图在按下按钮时显示警报
<script>
$("#login").on("click", function() {
alert("hai");
});
</script>
这也行不通
最佳答案
在元素出现在页面上之前,您正在执行 jQuery。在结束前将您的代码移动到页面末尾 </body>
标签,或将其包装在 document ready 中调用:
$( document ).ready(function() {
$("#login").click(function() {
alert("hai");
});
});
关于javascript - jquery click() 和 on() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43825118/