簡體   English   中英

jQuery取消綁定事件

[英]Jquery unbind events

我有兩個腳本

$(".MakeBid").on('click', function(e) {
        e.preventDefault();
        if ($('#pop_up').length) {$('#pop_up').html("").height('auto').width('auto').offset({left:0,top:0});}
        var strSrc = 'index.asp?id=' + $(this).attr('id');
        $.get(strSrc, function(data) {
            if ($('#pop_up').length) {
                $('<iframe src="' + strSrc + '"/>').appendTo('#pop_up');
                $('#pop_up').show(function() {
                    $('#pop_up').height(150).width(800);
                    $('#pop_up').offset({top:e.pageY,left:e.pageX});
                    $('#pop_up').on( "mouseleave", function() {
                        if ($('#pop_up').length) {
                            $('#pop_up').html("").height('auto').width('auto').offset({left:0,top:0});
                            $('#pop_up').hide(); 

                        }
                        $('#pop_up').unbind();
                    });
                    $('.authorize-close').click(function(e){      
                        e.preventDefault();
                        if ($('#pop_up').length) {
                            $('#pop_up').html("");
                            $('#pop_up').hide(); 
                        }
                    });
                });
            }
        });
$(".MakeBid").unbind('click');
}); 

$(".Login").on('click', function(e) {
        e.preventDefault();
        if ($('#pop_up').length) {$('#pop_up').html("").height('auto').width('auto').offset({left:0,top:0});} //restore div position and offset
        var strSrc = 'index.asp?id=' + $(this).attr('id');
        $.get(strSrc, function(data) {
            if ($('#pop_up').length) {
                $('<iframe src="' + strSrc + '"/>').appendTo('#pop_up');
                $('#pop_up').show(function() {
                    $('#pop_up').height(150).width(800); //set div size
                    $('#pop_up').offset({top:e.pageY,left:e.pageX}); // set div offset
                    $('.authorize-close').click(function(e){     //on close button press  
                        e.preventDefault();
                        if ($('#pop_up').length) {
                            $('#pop_up').html(""); //empty div
                            $('#pop_up').hide(); // hide div
                        }
                    });
                });
            }
        });
$(".Login").unbind('click');
}); 

因此,當我單擊.MakeBid $('#pop_up').on( "mouseleave",激活並關閉#pop_up后,單擊.Login $('#pop_up').on( "mouseleave",仍可正常使用。

如何解除綁定?

您必須修改$('#pop_up')。unbind(); 到$('#pop_up')。off(“ mouseleave”); 像下面的代碼

  $('#pop_up').on( "mouseleave", function() { if ($('#pop_up').length) { $('#pop_up').html("").height('auto').width('auto').offset({left:0,top:0}); $('#pop_up').hide(); } $('#pop_up').off("mouseleave"); }); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM