DOJO 八 event dojo/on
官网教程: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的更多相关文章
- Dojo API中文 Dojo内容模块概览,初学者
官网:http://dojotoolkit.org/reference-guide/1.10/dojo/index.html#dojo-dojo的翻译 dojo 内容: dojo dojo/dojo ...
- Dojo初探之4:dojo的event(鼠标/键盘)事件绑定操作(基于dojo1.11.2版本)
前言: 上一章详解了dojo的dom/query操作,本章基于dom/query基础上进行事件绑定操作 dojo的事件 dojo的事件绑定操作分为鼠标和键盘两种进行详解 1.鼠标事件 我们沿用上一章中 ...
- dojo.byId、dojo.query、dojo.attr
概述: dojo.byId(/*string*/id或/*DomNode*/node) 1.传入DOMNode返回传入的domNode; 2.传入id返回id为当前值的domNode dojo.que ...
- dojo 官方翻译 dojo/_base/lang 版本1.10
官方地址:http://dojotoolkit.org/reference-guide/1.10/dojo/_base/lang.html#dojo-base-lang 应用加载声明: require ...
- Dojo入门:dojo中的事件处理
JS为DOM添加事件 在原生的环境下,为DOM添加事件处理函数有多种方法: <input type="button" name="btn" value ...
- dojo.publish 和 dojo.subscribe
原文链接:http://www.cnblogs.com/didi/archive/2010/06/13/1757894.html //dojo.publish 和 dojo.subscribe :d ...
- dojo 十 ajax dojo/_base/xhr
官方教程:Ajax with DojoAjax功能:1.从服务器加载静态数据2.从web服务中访问xml或json类型的数据3.将表单(form)数据发送到服务器4.刷新页面内容....Ajax在RI ...
- dojo 九 effects dojo/_base/fx 和 dojo/fx
官方教程:Dojo Effects这里讲学习一下dojo如何实现淡入.淡出.滑动等效果.实现这些特殊的效果有两个包 dojo/_base/fx 和 dojo/fx.dojo/_base/fx 中提供了 ...
- dojo.js --dojo Quick Start/dojo入门手册1
我看了http://www.cnblogs.com/mylem/archive/2009/11/11/1600984.html这篇博客以后 ,就开始设计自己的代码,其实很多解释都是在我的代码里,所以就 ...
随机推荐
- 第五周&第六周
学习进度表 周数 专业学习目标 学习时间 新增代码行 博客发表量 人文方面学习 知识总结 第四周 认真掌握老师上课所讲的内容,在课外多学习一些知识 5小时 50 1 阅读 ...
- HDU 3555 Bomb 数位dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others) Mem ...
- 版本控制 - SVN/TortoiseSVN
研读了blog: 1. http://www.open-open.com/lib/view/open1346982569725.html 2. http://www.360doc.com/conte ...
- Swift-6-函数
// Playground - noun: a place where people can play import UIKit // 定义和调用函数 func sayHello(personName ...
- Xml Schema的用途
Xml Schema的用途 1. 定义一个Xml文档中都有什么元素 2. 定义一个Xml文档中都会有什么属性 3. 定义某个节点的都有什么样的子节点,可以有多少个子节点,子节点出现的顺序 4. ...
- 13test05:亲密数
/*#include<iostream> using namespace std; int main() {int sum[3000]={0}; for(int i=1;i<3000 ...
- c#实现串口操作 SerialPort
命名空间:using System.IO.Ports;该类提供了同步 I/O 和事件驱动的 I/O.对管脚和中断状态的访问以及对串行驱动程序属性的访问. 操作类声明: SerialPort sp = ...
- 小圣求职记B:总集篇
1. 搜狐sohu 搜狐在正式招聘前邀请了部分应聘者到武汉研发中心开座谈会(因此简历尽量早投,机会多些),有研发的也有产品的,40人左右,座谈会期间介绍了搜狐汽车.北京研发中心.武汉研发中心和搜狐媒体 ...
- POJ 1222
EXTENDED LIGHTS OUT Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6196 Accepted: 40 ...
- POJ 1665
#include<iostream>//chengdacaizi 08.11.12 #include<iomanip> #define p 3.1415927 using na ...