简体   繁体   English

jQuery无法在Ajax上加载

[英]jQuery does not load on Ajax

Im currently trying to do some work on some code I did not write or fully understand. 我目前正在尝试对一些我未编写或未完全理解的代码进行一些工作。 The page dynamically calls in content with AJAX. 该页面使用AJAX动态调用内容。 I am trying to manipulate that content but of course because the page has already been loaded when I apply it to the dynamic content it gets ignored. 我试图操纵该内容,但是当然是因为当我将页面应用于动态内容时,页面已经被加载,它会被忽略。 Here are some basic examples of the jQuery i'm trying to call: 这是我尝试调用的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');
}

How can I get this to work with ajax please? 我如何才能将此与ajax一起使用?

Use it in this way. 以这种方式使用它。

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

As you adding content dynamically it need event delegation . 在动态添加内容时,需要事件委托

UPDATE : 更新:

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

    //your script
 })

eg 例如

 $("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