在Ajax调用之后的init js

时间:2013-04-18 18:06:34

标签: ajax href

我有一个小的Javascript用于使用href链接交换图像。一切都很好,除非我使用Ajax在页面内调用它们。我需要找到一种方法来在调用新链接后再次初始化脚本。 这是JS:

<script type="text/javascript">

$( 'a.thumbnail')。点击(函数(){     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;

});

谢谢

1 个答案:

答案 0 :(得分:1)

您需要委派事件。

转过来:

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

进入这个:

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

确保您使用的是jQuery 1.7 +

相关问题