官网教程: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. BitMap排序

    问题描述:       BitMap排序思想:             用1bit位标记某个元素对应的值       优点:             效率高,不允许进行比较和移位            ...

  2. dancing link

    http://www.cnblogs.com/grenet/p/3145800.html 链接给的博客写的很好,比较好懂. 可惜不是c语言... 于是决定自己要建一个模板. 一道裸题:hustoj 1 ...

  3. 【POJ】【2699】The Maximum Number of Strong Kings

    网络流/最大流/二分or贪心 题目大意:有n个队伍,两两之间有一场比赛,胜者得分+1,负者得分+0,问最多有几只队伍打败了所有得分比他高的队伍? 可以想到如果存在这样的“strong king”那么一 ...

  4. 【BZOJ】【2756】【SCOI2012】奇怪的游戏

    网络流-最大流 这题……建模部分先略过 这道题是会卡时限的T_T俺的Dinic被卡了,在此放几篇很棒的讲网络流算法的文章,至于大家耳熟能详的论文就不放了…… http://www.cppblog.co ...

  5. [bzoj 3687]简单题 bitset的运用

    题意 给定一个正整数集,求所有子集算术和的异或和   题解 每次加入一个元素x,用原集合a xor (a<< x) 然后每一个值统计一下 bitset看起来很优越,是一个能位运算的布尔数组 ...

  6. 4-Highcharts 3D图之3D普通饼图

    <!DOCTYPE> <html lang='en'> <head> <title>4-Highcharts 3D图之3D普通饼图</title& ...

  7. [百度空间] [转] 四元数(Quaternions)

    转:四元数(Quaternions) 好吧,我必须承认到目前为止我还没有完全理解四元数,我一度把四元数理解为轴.角表示的4维向量,也就在下午我才从和同事的争辩中理解了四元数不完全是角.轴这么简单,为此 ...

  8. 负MARGIN之讲解

    css中的负边距(negative margin)是布局中的一个常用技巧,只要运用得合理常常会有意想不到的效果.很多特殊的css布局方法都依赖于负边距,所以掌握它的用法对于前端的同学来说,那是必须的. ...

  9. 用include来处理模板的问题

    /** * 测试方法 */ protected function getHtml() { $tpl = $this->pageletDir.$this->plTemplate; $html ...

  10. How Big Is A Petabyte, Exabyte, Zettabyte, Or A Yottabyte?

    一个关于Byte递增的描述,蛮有趣的! Bytes -> Kilobyte -> Megabyte -> Gigabyte -> Terabyte -> Petabyte ...