且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery:为什么我在li元素上的click事件不起作用?

更新时间:2023-11-12 08:01:34

该代码可以按预期工作,请查看 JSFiddle .

The code works fine as you expect, view the JSFiddle.

我有种感觉,就是您忘记了将其放入jQuery DOM Ready事件中.

I have a feeling your forgetting something like putting it in the jQuery DOM Ready event.

确保您的代码看起来像这样(在DOM Ready内)

Make sure you code looks like this (inside the DOM Ready)

$(document).ready(function(){ // When the DOM is Ready, then bind the click
    $("#nav ul li").click(function(){
        $("#nav").animate({ 
            marginLeft: "-300px",
        }, 1000 );
    });
});

确保页面上没有其他javascript错误(如果在此代码之前执行此操作,则该代码将不起作用),检查控制台(在Chrome浏览器中,右键单击>检查元素>控制台).

Make sure you have no other javascript errors on your page (If you do before this code, the code will not work), Check your console (In chrome right click > Inspect Element > console).

这两个问题之一(最有可能)是导致您出现问题的原因,否则您将不得不自行调试(或向我们显示更多代码).

One of these 2 issues are causing your problems (most likely), else you will have to debug it yourself (or show us more code).