2016-07-25 212 views
0

我有一個我創建的jquery插件,它顯示內容並隱藏內容在類似界面的選項卡中。我想要的是在顯示某些內容時觸發一個事件,然後我可以在我的源代碼中偵聽並根據此情況觸發事件,這是可能的?jquery插件添加事件監聽器

這裏是我的插件,

$.fn.appsTabs = function(options){ 

    // These are the defaults. 
    var settings = $.extend({ 
    // These are the defaults. 
    selected: "home" 
    }, options); 

    // Set Active 
    $(this).find('.pops-tabs-navigation li[data-route="'+settings.selected+'"]').addClass('active'); 

    // Hide All Boxes and show first one 
    $(this).find('.apps-tabs-content .pops-tab-content').addClass('cloak'); 
    $(this).find('.pops-tabs-content .pops-tab-content#content-'+settings.selected).removeClass('cloak'); 
    // Get Clicks on Li in Navigation 
    $(this).find('.apps-tabs-navigation li').click(function(){ 
    if($(this).hasClass('active') == false) { 
     // Get ID of Tab 
     id = $(this).attr('id'); 
     tabid = 'content-'+id; 

     // Set Active 
     $(this).parent('.apps-tabs-navigation').find('li').removeClass('active'); 
     $(this).addClass('active'); 

     // Hide all boxes 
     $(this).parent('.apps-tabs-navigation').parents('.pops-tabs').find('.pops-tabs-content .pops-tab-content').addClass('cloak'); 
     $(this).parent('.apps-tabs-navigation').parents('.pops-tabs').find('.pops-tabs-content #'+tabid).removeClass('cloak'); 
    } 
    }); 
} 

是否有可能在我的主應用程序代碼中可能添加了一些原型,聽是什麼?

回答

0

在你的插件代碼,你可以觸發一個自定義事件,並聽它在頁面的javascript:

插件

// Show your element (example) 
$('#myElement').show(); 
// Trigger custom event 
$(document).trigger('myCustomPluginEvent'); 

// Event listener for custom event 'myCustomPluginEvent' 
$(document).on('myCustomPluginEvent', function() { 
    // Do something when custom event occurs 
});