jquery 事件完成后取消委托点击事件

发布于 2024-09-15 02:25:20 字数 2049 浏览 5 评论 0原文

我有一个点击事件,可以将一些 AJAX 内容动画显示到页面上。

一旦此内容被显示并且用户单击激活该过程的同一链接,我现在想要反转该过程并关闭当前打开的飞出内容。

目前,通过单击“关闭”链接或单击序列中的另一个飞出链接可以关闭当前打开的飞出链接。如果用户单击当前的飞出链接,那么我希望当前的飞出链接关闭。

// Close fly out function
function closeFlyout() {

    $('.fly_container').animate({
        'right': '-332'
    }, 300, 'swing', function() {
        $(this).detach();
        /* TODO: z-index issues in IE7, IE6
        $('.dark_overlay').fadeOut(300, function() {
        $(this).remove();
        });
        */
    });

};

$('.widget').delegate('.widget .fly_out', 'click', function() {

    /*  
    TODO: z-index issues in IE7, IE6
    $('body').prepend('<div class="dark_overlay" />');
    */

    var $widget = $(this).closest('.widget');

    var $flyOutIndex = $(this).index('.fly_out');

    if ($flyOutIndex == 0) {
        $flyOutURL = 'Content/HTMLSnippets/Flyouts/Priceassessment/product_order.htm';
    } else if ($flyOutIndex == 1) {
        $flyOutURL = 'Content/HTMLSnippets/Flyouts/PriceHistory/price_history_comparisons.htm';
    } else if ($flyOutIndex == 2) {
        $flyOutURL = 'Content/HTMLSnippets/Flyouts/PriceHistory/price_history_scenarios.htm';
    } else if ($flyOutIndex == 3) {
        $flyOutURL = 'Content/HTMLSnippets/Flyouts/PriceHistory/price_history_analysis.htm';
    }

    $('.current').removeClass('current');

    $(this).addClass('current');

    // Close any open flyouts
    closeFlyout();

    $.ajax({
        type: 'GET',
        url: DashboardApplicationRoot + $flyOutURL,
        dataType: 'html',
        cache: true,
        success: function(data) {

            $($widget).prepend(data);

            $('.fly_container').animate({ 'right': '0' }, 300);

            $('.scroll').jScrollPane();

            $('.striped li:nth-child(even)').addClass('odd');

        }
    });

    return false;

});

// Close fly out function
$('.widget').delegate('.fly_container .close', 'click', function() {

    closeFlyout();

    $('.current').removeClass('current');

    return false;

});

I have a click event that animates in some AJAX content onto a page.

Once this content has been revealed and a user clicks the same link that activated the process i now want to reverse the process and close the currently open fly out content.

At the moment the currently open fly out is closed either by clicking a 'close' link or clicking another fly out link in the sequence. If a user clicks the current fly out link then i want the current fly out to close.

// Close fly out function
function closeFlyout() {

    $('.fly_container').animate({
        'right': '-332'
    }, 300, 'swing', function() {
        $(this).detach();
        /* TODO: z-index issues in IE7, IE6
        $('.dark_overlay').fadeOut(300, function() {
        $(this).remove();
        });
        */
    });

};

$('.widget').delegate('.widget .fly_out', 'click', function() {

    /*  
    TODO: z-index issues in IE7, IE6
    $('body').prepend('<div class="dark_overlay" />');
    */

    var $widget = $(this).closest('.widget');

    var $flyOutIndex = $(this).index('.fly_out');

    if ($flyOutIndex == 0) {
        $flyOutURL = 'Content/HTMLSnippets/Flyouts/Priceassessment/product_order.htm';
    } else if ($flyOutIndex == 1) {
        $flyOutURL = 'Content/HTMLSnippets/Flyouts/PriceHistory/price_history_comparisons.htm';
    } else if ($flyOutIndex == 2) {
        $flyOutURL = 'Content/HTMLSnippets/Flyouts/PriceHistory/price_history_scenarios.htm';
    } else if ($flyOutIndex == 3) {
        $flyOutURL = 'Content/HTMLSnippets/Flyouts/PriceHistory/price_history_analysis.htm';
    }

    $('.current').removeClass('current');

    $(this).addClass('current');

    // Close any open flyouts
    closeFlyout();

    $.ajax({
        type: 'GET',
        url: DashboardApplicationRoot + $flyOutURL,
        dataType: 'html',
        cache: true,
        success: function(data) {

            $($widget).prepend(data);

            $('.fly_container').animate({ 'right': '0' }, 300);

            $('.scroll').jScrollPane();

            $('.striped li:nth-child(even)').addClass('odd');

        }
    });

    return false;

});

// Close fly out function
$('.widget').delegate('.fly_container .close', 'click', function() {

    closeFlyout();

    $('.current').removeClass('current');

    return false;

});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵你手 2024-09-22 02:25:20

.delegate() 每次获取事件时都会检查选择器,所以你可以这样做:

$('.widget').delegate('.widget .fly_out:not(.foClose)', 'click', function() {
  $(this).addClass('foClose');
  //rest of current code
});

然后在你的关闭委托中也监听这个新的选择器:

$('.widget').delegate('.fly_container .close, .widget .foClose', 'click', function() {
  $(this).removeClass('foClose');
  //rest of current code
});

通过添加 foClose 类(或任何你想命名的名称),按钮的点击事件将由关闭处理代表倾听,而不是打开一个。如果以这种方式单击并处理,它将删除 foClass,使其再次成为弹出链接。

.delegate() checks the selector each time it gets an event, so you could do this:

$('.widget').delegate('.widget .fly_out:not(.foClose)', 'click', function() {
  $(this).addClass('foClose');
  //rest of current code
});

Then in your close delegate listen for this new selector as well:

$('.widget').delegate('.fly_container .close, .widget .foClose', 'click', function() {
  $(this).removeClass('foClose');
  //rest of current code
});

By adding the foClose class (or whatever you wish to name it) the button's click event would be handled by the closing delegate listening, rather than opening one. If it's clicked and handled that way, it'll remove the foClass, making it a flyout link again.

乖乖 2024-09-22 02:25:20

单击飞出按钮时,测试菜单是否具有 current 类。如果是这样,请关闭弹出窗口并且不要运行 ajax 方法。

if ($(this).hasClass("current")) { 
    $(this).removeCLass("current"); 
    closeFlyout(); 
    return; 
}

On a fly-out click, test to see if the menu has the class current. If it does, close the flyout and don't run the ajax method.

if ($(this).hasClass("current")) { 
    $(this).removeCLass("current"); 
    closeFlyout(); 
    return; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文