简体   繁体   English

toggle()对加载了ajax的内容不起作用?

[英]toggle() not working for content loaded with ajax?

$('.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. 这对于加载页面加载的内容效果很好,但是相同的功能不适用于使用ajax加载的内容,只是不会拦截点击。

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
})

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'));
});

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

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