切换()不工作的装载Ajax内容? [英] toggle() not working for content loaded with ajax?

查看:128
本文介绍了切换()不工作的装载Ajax内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $('。slideArrow)切换(函数(事件){
  //一些code
},功能(事件){
    //一些code
});
 

这为它们装上页load.But相同的功能不适用于装载ajax.It只是不拦截点击内容工作内容工作正常。

我应该怎么办?

在其他情况下,我面临着同样的问题(不适用于切换,对于点击),整理它这个way.I不知道该怎么做切换?

  $('。共父')。在('点击','。目标-的点击',函数(){
//一些code
})
 

解决方案

标志方法:

  VAR标志= FALSE;

$(文件)。在('点击','.slideArrow',函数(事件){
    如果(旗){
        //做一件事
    }其他{
        //做另一件事
    }
    标志=标志!;
});
 

中的数据的方法

  $(文件)。在('点击','.slideArrow',函数(事件){
    如果($(本)的.d​​ata('标志')){
        //做一件事
    }其他{
        //做另一件事
    }

    $(本)的.d​​ata('标志',$(本)的.d​​ata('标志')!);
});
 

$('.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
})

解决方案

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

这篇关于切换()不工作的装载Ajax内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆