javascript - jquery click() 和 on() 不工作

标签 javascript jquery html click

在下面的代码中,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/

相关文章:

javascript - 如何使用属性对 Javascript 函数对象进行字符串化?

javascript - 将输入字符串与文本/元素匹配并突出显示 react 性

Javascript - 按下按键后保持按下状态(返回 true)

javascript - 如何在不设置 setInterval 的情况下激活我的进度条

Javascript/JQuery 忽略更改事件的 stopPropagation

javascript - 如何在javascript中的数组中的特定位置插入一个div

javascript - 四舍五入到小数点后两位,javascript

javascript - React Native 获取并渲染 json

javascript - Web 2.0 生态系统/堆栈

c# - 自定义 ASP.NET 控件中的 Begin/EndRequestHandler 多个处理程序