我的代码如下: '''======================================
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Title</title> </head><script>
    //绑定‘单击事件’的 jQuery
    $('#all_move1').click(function(){
    alert('AA');
    })
    //绑定‘单击事件’的 jQuery
    $('#all_move2').bind('click',function(){
    alert('BB');
    })
    //绑定‘单击事件’的 javascript
    document.getElementById('all_move1').onclick=function(){
        alert('CC');
    }
    //函数式
    function danji(){
        alert('DD');
    }
</script>
'''=============================
这里为什么发现只有“点击按钮 2”,点击了之后会有 alert 。 只有函数式的方法可以生效, 为什么前面三种方法不行呢? 我用的 chrome 浏览器
|  |      1Building      2021-09-04 11:30:15 +08:00 via iPhone 因为你把 script 放在了 body 前面。 | 
|  |      2kop1989      2021-09-04 11:33:28 +08:00 via iPhone 绑的太早了。 js 执行绑定的时候,真正的 dom 还没有出现。 所以你这个点击事件相当于绑了个寂寞。 要么将绑定逻辑放在 ready 事件的回调中,要么使用 html 的 onclick 声明。 | 
|  |      3kop1989      2021-09-04 11:36:59 +08:00 btw 你并没有引用 Jquery 的库,所以第一行的$就已经报错了。 | 
|  |      4FragmentLs      2021-09-04 11:37:20 +08:00 1 & 2: 因为你没引入 jquery...... 3: 你的 js 没 defer | 
|      5Rache1      2021-09-04 11:41:21 +08:00 你这不只是给 2 绑定了一个事件吗,而且你这个还是 DOM 的前面绑定的,JS 执行的时候都没有这个元素,实际上都是没绑上 打印 1 还是 2,取决于 JS 片段放的位置。 http://jsrun.net/gZTKp/edit | 
|  |      6loading      2021-10-23 17:13:20 +08:00 用了 jQuery ,不知道 $().ready(). ? |