Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
69 views
in Technique[技术] by (71.8m points)

JQuery为何不能运行?

初次调用JQuery的$()好像没有任何效果。例如在下面的实例中,点击按钮后能够正常弹窗:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>测试网页</title>
        <script src="jquery-3.5.1.js"></script>
        <script>
            window.onload = function() {
                var button = document.getElementById("button");
                button.onclick = function() {
                    alert("hello");
                }
            }
        </script>
    </head>
    <body>
        <input type="button" value="按钮" id="button" />
    </body>
</html>

但如果换成对JQuery的调用就无法弹窗:

<script>
    window.onload = function() {
        $("#button").onclick = function() {
            alert("hello");
        }
    }
</script>

代码看了几遍也没看出问题。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
<script> window.onload = function() {
    $("#button")./*on*/click(/* = */function() {
        alert("hello");
    });
} </script>

或者:

<script> window.onload = function() {
    $("#button")[0].onclick = function() {
        alert("hello");
    }
} </script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...