jQuery不会加载Ajax

时间:2016-01-20 09:25:30

标签: javascript jquery ajax

我目前正在尝试对一些我没写或完全理解的代码做一些工作。该页面使用AJAX动态调用内容。我正在尝试操作该内容,但当然因为当我将页面应用于动态内容时它已被加载它被忽略。以下是我试图调用的jQuery的一些基本示例:

$(".check").each(function() {
    $(this).hide();
    var $image = $("<img src='img/checked.png' />").insertAfter(this);
    $image.click(function() {
        var $checkbox = $(this).prev(".check");
        $checkbox.prop('checked', !$checkbox.prop('checked'));

        if ($checkbox.prop("checked")) {
            $image.attr("src", "img/checked.png");
        } else {
            $image.attr("src", "img/unchecked.png");
        }
    })
});

if ($(window).width() < 500) {
    $('.panel-collapse').removeClass('in');
} else {
    $('.panel-collapse').addClass('in');
}

我怎样才能让它与ajax一起使用?

1 个答案:

答案 0 :(得分:1)

以这种方式使用它。

$image.on('click',function() {
        // your script 
})

在动态添加内容时,需要事件delegation

更新:

 $("some_parent_id_or_class").on('click','img_with_some_class_or_id',function(){

    //your script
 })

e.g。

 $("some_parent_id_or_class").on('click','img',function(){

    //your script
 })