JQuery单击事件,无法正常工作

Rol*_*and 4 javascript jquery

我有以下场景.

我有一个index.php页面,其中包含以下JQuery代码

jQuery(document).ready(function(){
    jQuery('#sIMG img').click(function() {
        var currentSRC = jQuery(this).attr('src');
        var altSRC = jQuery(this).attr('title');
        var imgID = jQuery(this).attr('id');
        var cat = jQuery(this).attr('name');


        /*Fade, Callback, swap the alt and src, fade in */
        jQuery('#main').fadeOut('fast',function() {
            jQuery('#main').load("detail.php?id="+imgID+"&category="+cat);
            jQuery('#main').fadeIn('fast');

         });
    });
});
Run Code Online (Sandbox Code Playgroud)

现在我在index.php页面中有两个名为#main和#right的div标签.当我点击一个菜单项时,右键更改为一堆图像,如果我点击其中一个图像,上面的代码应该生效并加载到主div中,但它只是不起作用.图像位于名为sIMG的div中.任何帮助将不胜感激

rah*_*hul 12

尝试使用直播

jQuery('#sIMG img').live("click",function(){
});
Run Code Online (Sandbox Code Playgroud)

从jQuery 1.7开始,不推荐使用.live()方法.使用.on()附加事件处理程序.

jQuery('#sIMG img').on("click",function(){
});
Run Code Online (Sandbox Code Playgroud)

  • 关闭jQuery 1.7你应该使用.on()而不是live(). (4认同)