查询单击上的Dojo在切换按钮的工具栏中不起作用

时间:2013-10-02 11:01:44

标签: toolbar dojo

按照示例here我尝试过这样做,但查询对我不起作用。

http://jsfiddle.net/qDbd5/

require(["dojo/parser", "dijit/Toolbar", "dijit/form/ToggleButton", "dojo/query", "dojo/dom-class", "dojo/on", "dojo/domReady!"], function (parser, ToolBar, ToggleButton, query, domClass, on) {
on(query(".dijitToggleButton"), "click", function (e) {
    query(".dijitToggleButton").forEach(function (node) {
        console.log('Captured clicked event');
        domClass.remove(node, "dijitToggleButtonChecked dijitToggleButtonRtlChecked dijitRtlChecked dijitChecked");
    });
    domClass.add(this, "dijitToggleButtonChecked dijitToggleButtonRtlChecked dijitRtlChecked dijitChecked");
});

});

我试图一次只做一个按钮切换。 为什么没有触发点击事件?

1 个答案:

答案 0 :(得分:1)

在ready()回调中包装所有内容可以解决问题。

请参阅http://jsfiddle.net/cFQGq/

require(["dojo/ready", "dojo/parser", "dijit/Toolbar", "dijit/form/ToggleButton", "dojo/query", "dojo/dom-class", "dojo/on", "dojo/domReady!"], function (ready, parser, ToolBar, ToggleButton, query, domClass, on) {
    ready(function() {
        on(query(".dijitToggleButton"), "click", function (e) {
            query(".dijitToggleButton").forEach(function (node) {
                console.log('Captured clicked event');
                domClass.remove(node, "dijitToggleButtonChecked dijitToggleButtonRtlChecked dijitRtlChecked dijitChecked");
            });
            domClass.add(this, "dijitToggleButtonChecked dijitToggleButtonRtlChecked dijitRtlChecked dijitChecked");
        });
    }); 
});

如果查看explanation of domReady !,它会提到它不足以使用dojo小部件,因为它在DOM加载后执行,而不是在小部件完成初始化之后执行。在小部件加载完成后,dojo / ready执行回调。