Dijit TabContainer Events - onFocus

2020-07-19 05:43发布

问题:

I have a Tab Container and I want to execute some js when I click on a tab's title bar.

I can't seem to figure out how to add an event to that.


EDIT:

It looks like I'll be using onFocus, but I'm still having trouble finding the proper syntax.


EDIT: Found onFocus and onBlur, but still having trouble getting it to work.

回答1:

You need to connect to the _transition event.

var tabs = dijit.byId("tabs");
dojo.connect(tabs,"_transition", function(newPage, oldPage){
    console.log("I was showing: ", oldPage || "nothing");
    console.log("I am now showing: ", newPage);
});

Where "tabs" is your TabContainer.



回答2:

Here's a complete code sample that works in Dojo 1.8, I've tested it:

require(["dijit/registry",  "dojo/on", "dojo/ready", "dojo/domReady!"], function (registry, on, ready) {
    ready(function () { //wait till dom is parsed into dijits
        var panel = registry.byId('mainTab');   //get dijit from its source dom element
        on(panel, "Click", function (event) {   //for some reason onClick event doesn't work 
            alert(panel.selectedChildWidget.id);  
        });
    });
});


回答3:

The correct Dojo 1.7+ should be;

var tabs = registry.byId('someTabs');
tabs.watch("selectedChildWidget", function(name, oval, nval){
    console.log("selected child changed from ", oval, " to ", nval);
});

another way could be

var tabs = registry.byId('someTabs');
aspect.after(tabs, "selectChild", function (event) {
     console.log("You selected ", tabs.selectedChildWidget.id);
});