官网教程:Events with Dojo
在元素上绑定events,需要引用包dojo/on,通过on方法来实现。

<button id="myButton">Click me!</button>
<div id="myDiv">Hover over me!</div>

require(["dojo/on",
"dojo/dom",
"dojo/dom-style",
"dojo/mouse",
"dojo/domReady!"],
    function(on, dom, domStyle, mouse)
{
        var myButton =
dom.byId(
"myButton"),
            myDiv =
dom.byId(
"myDiv");
 
        on(myButton, "click",
function(evt){
            domStyle.set(myDiv,
"backgroundColor",
"blue");
        });
        on(myDiv, mouse.enter,
function(evt){
            domStyle.set(myDiv,
"backgroundColor",
"red");
        });
        var handler = on(myDiv, mouse.leave,
function(evt){
            domStyle.set(myDiv,
"backgroundColor",
"");
        });
       
handler.remove();//移除event

       
        on.once(
myDiv,
mouse.leave,
function(evt){

            domStyle.set(myDiv,
"backgroundColor",
"");
        });
});

on方法不需要前缀。包括三个参数。
第一个:需要绑定events的元素
第二个:event名称
第三个:处理event的方法体,这个方法体有只一个参数,为event的对象,包括一些属性和方法,如果需要传递其他参数,将在后面讲到。
方法on的返回值是一个简单的对象,只有一个remove方法,执行这个方法,元素就会移除这个event。
还有一个方法on.once(element,event
name,handler),参数同on一样,这个方法顾名思义就是只执行一次,在执行了handler后将会自动remove。
一个元素可以绑定多个events,每个event按照绑定的
先后顺序来执行的。

----------------------------------

require(["dojo/on",
"dojo/dom",
"dojo/_base/lang",
"dojo/domReady!"],
    function(on, dom, lang) {
 
        var myScopedButton1 =
dom.byId(
"myScopedButton1"),
            myScopedButton2 =
dom.byId(
"myScopedButton2"),
            myObject = {
                id:
"myObject",
                onClick:
function(arg){
                    alert("The scope of
this handler is "
+ this.id + " , value = " +
arg
);
                }
            };
 
        // This will alert
"myScopedButton1"
        on(myScopedButton1,
"click", myObject.onClick);
        // This will alert "myObject" rather than
"myScopedButton2"
        on(myScopedButton2,
"click", lang.hitch(myObject, "onClick",
"something"
));
 
});
handler可以是一个方法体,也可以是一个对象里定义的方法,这个方法可以带多个参数。
如果handler表示为object.method,那么无法传递参数,而method中的this指代的是当前的元素。
如果handler用方法lang.hitch(object,method
name,[arg1,[arg2,.....]])来表示,则可能传递N个参数,但method中的this指代的是object。
define(["dojo/aspect"], function(aspect){
aspect.after(dojo, "xhr", function(deferred){
// this is called after any dojo.xhr call
});
// this will execute the original dojo.xhr method and then our advising function
dojo.xhr("GET", {...});
});
define(["dojo/aspect"], function(aspect){
aspect.before(dojo, "xhr", function(method, args){
// this is called before any dojo.xhr call
if(method == "PUT"){
// if the method is PUT, change it to a POST and put the method in the parameter string
args.url += "?x-method=PUT";
// return the new args
return ["POST", args];
}
});
// this will execute the original our advising function and then dojo.xhr
dojo.xhr("PUT", {...});
});
define(["dojo/aspect"], function(aspect){
aspect.around(dojo, "xhr", function(originalXhr){
return function(method, args){
// doing something before the original call
var deferred = originalXhr(method, args);
// doing something after the original call
return deferred;
}
});
dojo.xhr("PUT", {...});
});

DOJO 八 event dojo/on的更多相关文章

  1. Dojo API中文 Dojo内容模块概览,初学者

    官网:http://dojotoolkit.org/reference-guide/1.10/dojo/index.html#dojo-dojo的翻译 dojo 内容: dojo dojo/dojo ...

  2. Dojo初探之4:dojo的event(鼠标/键盘)事件绑定操作(基于dojo1.11.2版本)

    前言: 上一章详解了dojo的dom/query操作,本章基于dom/query基础上进行事件绑定操作 dojo的事件 dojo的事件绑定操作分为鼠标和键盘两种进行详解 1.鼠标事件 我们沿用上一章中 ...

  3. dojo.byId、dojo.query、dojo.attr

    概述: dojo.byId(/*string*/id或/*DomNode*/node) 1.传入DOMNode返回传入的domNode; 2.传入id返回id为当前值的domNode dojo.que ...

  4. dojo 官方翻译 dojo/_base/lang 版本1.10

    官方地址:http://dojotoolkit.org/reference-guide/1.10/dojo/_base/lang.html#dojo-base-lang 应用加载声明: require ...

  5. Dojo入门:dojo中的事件处理

      JS为DOM添加事件 在原生的环境下,为DOM添加事件处理函数有多种方法: <input type="button" name="btn" value ...

  6. dojo.publish 和 dojo.subscribe

    原文链接:http://www.cnblogs.com/didi/archive/2010/06/13/1757894.html //dojo.publish 和 dojo.subscribe  :d ...

  7. dojo 十 ajax dojo/_base/xhr

    官方教程:Ajax with DojoAjax功能:1.从服务器加载静态数据2.从web服务中访问xml或json类型的数据3.将表单(form)数据发送到服务器4.刷新页面内容....Ajax在RI ...

  8. dojo 九 effects dojo/_base/fx 和 dojo/fx

    官方教程:Dojo Effects这里讲学习一下dojo如何实现淡入.淡出.滑动等效果.实现这些特殊的效果有两个包 dojo/_base/fx 和 dojo/fx.dojo/_base/fx 中提供了 ...

  9. dojo.js --dojo Quick Start/dojo入门手册1

    我看了http://www.cnblogs.com/mylem/archive/2009/11/11/1600984.html这篇博客以后 ,就开始设计自己的代码,其实很多解释都是在我的代码里,所以就 ...

随机推荐

  1. VC6.0装了visual assist x回车键不能补全代码的解决方法

    问题:VC6.0装了visual assist x补全代码具体怎么用?         输入字母后会像输入法那样出现一个菜单        但是怎么选择菜单里面的内容呢?        什么 回车  ...

  2. python 关键字参数

    原文地址:http://docs.pythontab.com/python/python3.4/controlflow.html#tut-functions 函数可以通过 关键字参数 的形式来调用,形 ...

  3. hibernate---核心开发接口1(重点)

    面试考这个比较少 a) Session session = sessionFactory.openSession();    永远都是打开新的 记得要 close b)  Session sessio ...

  4. draw call 的优化

    用一张贴图,renderstate都设置成一致,多个draw合并成一个

  5. IE6 IE7: div中table宽度100%导致的宽度问题

    问题现象:定义了DOCTYPE的页面 当表格的内容比div的高度还要高时,div会出现滚动条,同时在IE6和IE7下会出现问题: IE6:此时table的100%宽度还是没有滚动条那是的宽度,出现滚动 ...

  6. SQLite中的日期基础

    SQLite包含了如下时间/日期函数: datetime().......................产生日期和时间 date()...........................产生日期 t ...

  7. C# 使用 AutoResetEvent 或 ManualResetEvent 同步两个线程

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. HDU 2671 Can't be easier(数学题,点关于直线对称)

    题目 //数学题//直线 y = k * x + b//直线 ax+by+c=0; 点 (x0,y0); 点到直线距离 d = (ax0+by0+c)/sqrt(a^2+b^2) /********* ...

  9. iOS获取手机相关信息

    iOS具体的设备型号: #include <sys/types.h> #include <sys/sysctl.h> - (void)test { //手机型号. size_t ...

  10. Es使用。

    http://jingyan.baidu.com/article/3052f5a1e8a06397f31f8699.html --------------------------- http://el ...