1

I have a small Javascript used to swap images using a href links. Everything is OK except when I call those a ref inside the page using Ajax. I need to find a way to initialize again the script after calling new links. Here is the JS:

<script type="text/javascript">

$('a.thumbnail').click(function(){ var src = $(this).attr('href');

if (src != $('img#largeImg').attr('src').replace(/\?(.*)/,'')){
    $('img#largeImg').stop().animate({
        opacity: '0'
    }, function(){
        $(this).attr('src', src+'?'+Math.floor(Math.random()*(10*100)));
    }).load(function(){
        $(this).stop().animate({
            opacity: '1'
        });
    });
}
return false;

});

Thank you

4

1 回答 1

1

您需要委托事件。

转这个:

$('a.thumbnail').click(function(){ /*...*/ });

进入这个:

$('#someWrapperContainer').on('click', 'a.thumbnail', function(){ /*...*/ });

确保您使用的是 jQuery 1.7+

于 2013-04-18T18:11:15.350 回答