开发者

JQuery click event, not working

开发者 https://www.devze.com 2022-12-23 00:14 出处:网络
I have the following scenario. I have a index.php page with the following JQuery code included jQuery(document).ready(function(){

I have the following scenario.

I have a index.php page with the following JQuery code included

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');

         });
    });
});

Now I have two div tags called #main and #right in the index.php page. When I click on a menu item right changes to a bunch of images, if I click on one of those images the above code should take effect and load into the main d开发者_JAVA技巧iv, but it's just not working. the images are located within a div called sIMG. Any help will be appreciated


Try using live

jQuery('#sIMG img').live("click",function(){
});

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers.

jQuery('#sIMG img').on("click",function(){
});


I think what you're doing is setting "click" on the array that is return there. Try this:

jQuery('#sIMG img').each(function() {
    jQuery(this).click(function() {

    });
});


One of the reasons that the jquery click don't work is that you have dupplicates id's in the form.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号