jQuery events not happening after AJAX load?

前端 未结 8 698
别跟我提以往
别跟我提以往 2021-01-22 13:59

I have a website that will fade out a section of my website, load new content in, and fadeIn. There\'s a feature I had where a picture would get big when you click on it (it\'s

8条回答
  •  余生分开走
    2021-01-22 14:21

    .click() is a shorthand of .bind('click'), which only binds to elements that are already in the page

    To bind events to current and future elements, you must use .live()

    // If the user makes a picture bigger
    $(".makePictureBig").live('click',function() {
         //code
    }); 
    

    Edit:

    As of jQuery 1.7, live is deprecated. You can use jQuery on:

    $(".makePictureBig").on('click', function() {
        //code
    }); 
    

    Or, for delegated event handlers:

    $("#wrapper").on('click', '.makePictureBig', function(){
        //code
    });
    

提交回复
热议问题