toggle() not working for content loaded with ajax?

馋奶兔 提交于 2019-11-27 08:28:31

问题


$('.slideArrow').toggle(function (event) {
  //some code
}, function (event) {
    //some code
});

This works fine for content which are loaded on page-load.But the same function does not work for content loaded with ajax.It just does not intercept the click.

What should I do?

In an other scenario,i faced a same problem(not for toggle,for click) and sorted it this way.I dont know what to do for toggle?

$('.common-parent').on('click','.target-of-click',function(){
//some code
})

回答1:


The flag method :

var flag = false;

$(document).on('click', '.slideArrow', function(event) {
    if (flag) {
        // do one thing
    }else{
        // do another thing
    }
    flag = !flag;
});

the data method

$(document).on('click', '.slideArrow', function(event) {
    if ( $(this).data('flag') ) {
        // do one thing
    }else{
        // do another thing
    }

    $(this).data('flag', !$(this).data('flag'));
});


来源:https://stackoverflow.com/questions/20032913/toggle-not-working-for-content-loaded-with-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!