繁体   English   中英

jQuery无法在Ajax上加载

[英]jQuery does not load on 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一起使用?

以这种方式使用它。

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

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

更新:

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

    //your script
 })

例如

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

    //your script
 })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM