包含$(this)的变量在html更改后返回undefined(仅在IE中)

时间:2014-01-28 09:33:33

标签: javascript jquery ajax

我有关于jQuery的问题。我正在使用ajax进行分页,以便用户单击页码并将该页面加载到另一个div中并在成功时显示。但我遇到了一个小问题。

我正在寻呼机链接点击上创建一个新变量,其中包含$(this)以供将来参考。但问题是,在成功功能中,我正在使用.html()替换新的html。在Chrome和Firefox中一切正常,但在IE中(在9和10上测试)我的变量在替换了寻呼机html后返回'undefined'。

现在我有点迷失了,因为我的变量不应该包含对$(this)的引用,即使在替换了pagers html之后?我不知道如何更好地解释这个,所以我希望有人理解我的问题:)

这是链接点击的代码

$('#pager').on('click', 'a', function(event){
event.preventDefault();

current_page = ($('#pager .current').index())+1;
var dis = $(this);

if(dis.parents('.pager-mid').length > 0) {
    var link_number = parseInt($(this).text());
}

if(dis.attr('href') != '#') {
    $.ajax({
        type:'get',
        url:dis.attr('href'),
        dis:dis,
        beforeSend:function(){
            $('<div class="text-center wait-remove"><div class="wait"></div></div>').insertBefore($('#pager'));
        },
        success:function(data){
            $('#pager').html($(data).find('#pager').html());
            $('.wait-remove').remove();
            var check_html = $(data).find('.galleries-page').html(),
                check_error = false;

            if(!check_error) {
                if(link_number) {
                    $('.galleries-page:eq('+(link_number-1)+')').html($(data).find('.galleries-page').html());
                } else {
                    if(dis.hasClass('next')) {
                        $('.galleries-page:eq('+(current_page)+')').html($(data).find('.galleries-page').html());
                    } else {
                        $('.galleries-page:eq('+(current_page-2)+')').html($(data).find('.galleries-page').html());
                    }
                }
            }



            if(dis.parents('.pager-mid').length > 0) {
                $('#galleries-inner').stop(true, false).animate({
                    'left':'-'+((link_number-1)*page_width)+'px'
                },600);
            } else {
                if(dis.hasClass('next')) {
                    $('#galleries-inner').stop(true, false).animate({
                        'left':'-'+((current_page)*page_width)+'px'
                    },600);
                } else {
                    $('#galleries-inner').stop(true, false).animate({
                        'left':'-'+((current_page-2)*page_width)+'px'
                    },600);
                }
            }
        }
    });
}
});

1 个答案:

答案 0 :(得分:0)

IE缓存ajax请求存在已知问题。设置缓存参数。

$.ajax({
    cache: false,
    // all your options/callbacks
});