2013-10-02 58 views
0

繼示例here我試過做同樣的事情,但查詢不適用於我。Dojo查詢點擊在切換按鈕的工具欄上不起作用

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

在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在部件完成加載後執行回調。

+0

我沒有找到一個bug。在你更新的JSFiddle中,如果我點擊複製,比點擊粘貼和複製HOVER ...它得到切換樣式althoug我沒有點擊它。任何想法爲什麼? – Alophind

+0

我已經解決了這個錯誤。 還需要在dijit項目中設置「checked = false」。 – Alophind