简体   繁体   中英

Detecting Tabs using tabsactivate - jQuery UI

I am trying to bind an event listener to tab activate event of jQuery

$( ".selector" ).on( "tabsactivate", function( event, ui ) {} );

I am not able to figure out how to make the above function work based on the active tab. I can do it by binding a click event to the tab, but it doesn't work when i use an arrow key.

This is the fiddle of it,

http://jsfiddle.net/aslums/CnEUh/3115/

$("#tabs").tabs({
    activate: function (event, ui) {
        var active = $('#tabs').tabs('option', 'active');
        console.log("Active .."+active)
    }
}
);

$( ".contentTab" ).on( "tabsactivate", tabEnabled);

function tabEnabled(){
console.log("here...")
}

The aim is to trigger a function when the tab is active

Kindly let me know what am missing.

You could use a custom event:

var tabsClicked = []; // keep track of clicked tab indexes

activate: function (event, ui) {
    …
    // Only fire custom event the first time tab is active
    if ($.inArray(active, tabsClicked) === -1) {
      tabsClicked.push(active);        
      $(document).trigger("custom-event", [{tab_index : active}]);          
    }
}

$(document).on("custom-event", customEventHandler);

function customEventHandler(event, data) {
  console.log("Tab: " + (++data.tab_index) + " was clicked");;
}

http://jsfiddle.net/CnEUh/3146/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM