1.普通綁定
dojo.event.connect(DOMNode, 'onclick', 'func');
2.綁定多個事件
dojo.event.connect(DOMNode, 'onclick', 'func1');
dojo.event.connect(DOMNode, 'onclick', 'func2');
3.綁定一個obj的兩個方法
dojo.event.connect(DOMNode, 'onclick', evtObj, 'func1');
dojo.event.connect(DOMNode, 'onclick', evtObj, 'func2');
4.讓綁定的事件順序執行
var exampleObj = {
counter: 0,
foo: function(){
alert("foo");
this.counter++;
},
bar: function(){
alert("bar");
this.counter++;
}
};
dojo.event.connect(exampleObj, "foo", exampleObj, "bar"); // 調用foo的話bar也會執行
這樣也可以
dojo.event.kwConnect({
type : 'before' // 讓bar在foo之前執行
srcObj : exampleObj,
srcFunc : 'foo',
targetObj : exampleObj,
targetFunc : 'bar',
delay : 2000 // bar延遲執行2秒
once : true // 綁定一次
});
exampleObj.foo();
5.先后執行
dojo.event.connect("after", exampleObj, "foo", exampleObj, "bar"); // bar在foo之后執行
dojo.event.connect("before", exampleObj, "foo", exampleObj, "bar"); // bar在foo之前執行
6.傳遞參數
var evtObj = function() {
this.func1 = function(a) {
return(function(e) {
alert((e||window.event).type);
})
};
this.func2 = function(b) {
alert(b);
};
}
var o = new evtObj();
var btn = dojo.byId("testBtn");
dojo.event.connect(btn, 'onclick', o.func1('hello'));
以前一般這樣寫:
function init() {
var evtObj = function() {
this.func1 = function(a) {
alert(a.type);
};
this.func2 = function(b) {
alert(b);
};
}
var o = new evtObj();
var btn = dojo.byId("testBtn");
dojo.event.connect(btn, 'onclick', function(event){o.func1(event)});
這樣的話你在第三個參數就要傳遞event,很麻煩