addEvent(type,fn):为DOM元素增加一个事件监听器

removeEvent(type,fn):移除先前为DOM元素添加的事件监听器

eg:

var destroy = function(){ alert('Boom: ' + this.id); } // this refers to the Element.
$('myElement').addEvent('click', destroy); //later...
$('myElement').removeEvent('click', destroy);

addEvents(events):一次为一个DOM元素添加多个事件监听器

eg:

$('myElement').addEvents({
mouseover: function(){
alert('mouseover');
},
click: function(){
alert('click');
}
});

removeEvents(events):移除DOM元素上某个类型的事件的所有监听器

var myElement = $('myElement');
myElement.addEvents({
mouseover: function(){
alert('mouseover');
},
click: function(){
alert('click');
}
}); myElement.addEvent('click', function(){ alert('clicked again'); });
myElement.addEvent('click', function(){ alert('clicked and again :('); });
//addEvent will keep appending each function.
//Unfortunately for the visitor, there will be three alerts they'll have to click on.
myElement.removeEvents('click'); // saves the visitor's finger by removing every click event.

fireEvent(type[,param,delay]):执行一个DOM元素上某个类型的事件的所有监听器,多用于需要在特定的时间或者场合下触发事件时

eg1:

function test(a) {
console.log(a);
}
$("btn1").addEvent('click', test);
$("btn1").addEvent('click', function() {
console.log('btn1 click event run')
}); $("btn1").fireEvent("click", "hello,world");

这个例子中代码将在页面一加载完就执行,而不需要等到click btn1时才执行。下面这个例子将更好的说明fireEvent的使用场合:

textarea.addEvents({
focus: function() {
// When focusing, if the textarea contains value "Type here", we
// simply clear it.
if (textarea.value.contains('Type here')) textarea.set('value', '');
}, keyup: function() {
// When user keyups we check if there are any of the magic words.
// If yes, we fire our custom event burn with a different text for each one.
if (textarea.value.contains('hello')) textarea.fireEvent('burn', 'hello world!');
else if (textarea.value.contains('moo')) textarea.fireEvent('burn', 'mootools!');
else if (textarea.value.contains('pizza')) textarea.fireEvent('burn', 'Italy!');
else if (textarea.value.contains('burn')) textarea.fireEvent('burn', 'fireEvent');
// note that in case of 'delayed', we are firing the event 1 second late.
else if (textarea.value.contains('delayed')) textarea.fireEvent('burn', "I'm a bit late!", 1000);
},
burn: function(text) {
// When the textarea contains one of the magic words
// we reset textarea value and set the log with text
textarea.set('value', '');
log.set('html', text);
// then we start the highlight morphing
highlight.start({
backgroundColor: ['#fff36f', '#fff'],
opacity: [1, 0]
});
}
});

注意标红部分。

结合这两个例子,想必fireEvent的用法已经很清楚了。

cloneEvent(from[,type]):从一个DOM元素上复制所有监听器到本元素:

eg:

function test(a) {
console.log(a);
}
$("btn1").addEvent('click', test);
$("btn1").addEvent('click', function() {
console.log('btn1 click event run')
});
$("btn1").fireEvent("click", 123);
$("btn2").cloneEvents($("btn1"));

将btn1上绑定的事件复制给btn2

Element.Event的更多相关文章

  1. DOJO 八 event dojo/on

    官网教程:Events with Dojo在元素上绑定events,需要引用包dojo/on,通过on方法来实现. <button id="myButton">Clic ...

  2. zepto源码学习-04 event

    之前说完$(XXX),然后还有很多零零碎碎的东西需要去分析,结果一看代码,发现zepto的实现都相对简单,没有太多可分析的.直接略过了一些实现,直接研究Event模块,相比JQuery的事件系统,ze ...

  3. Html5 touch event

    HTML5 for the Mobile Web: Touch Events POSTED BY RUADHAN - 15 AUG 2013 See also... Touch-friendly Dr ...

  4. Zepto源码分析-event模块

    源码注释 // Zepto.js // (c) 2010-2015 Thomas Fuchs // Zepto.js may be freely distributed under the MIT l ...

  5. 读Zepto源码之Event模块

    Event 模块是 Zepto 必备的模块之一,由于对 Event Api 不太熟,Event 对象也比较复杂,所以乍一看 Event 模块的源码,有点懵,细看下去,其实也不太复杂. 读Zepto源码 ...

  6. 一个普通的 Zepto 源码分析(三) - event 模块

    一个普通的 Zepto 源码分析(三) - event 模块 普通的路人,普通地瞧.分析时使用的是目前最新 1.2.0 版本. Zepto 可以由许多模块组成,默认包含的模块有 zepto 核心模块, ...

  7. JavaScript Interview Questions: Event Delegation and This

    David Posin helps you land that next programming position by understanding important JavaScript fund ...

  8. zepto源码分析·event模块

    准备知识 事件的本质就是发布/订阅模式,dom事件也不例外:先简单说明下发布/订阅模式,dom事件api和兼容性 发布/订阅模式 所谓发布/订阅模式,用一个形象的比喻就是买房的人订阅楼房消息,售楼处发 ...

  9. Event Binding in Angular

    https://www.pluralsight.com/guides/angular-event-binding Introduction In this guide, we will explore ...

随机推荐

  1. Ajax辅助方法

    目前为止,我们已经考察了如何编写客户端JavaScript代码,以发送并接受服务器的数据.然而,在使用ASP.NET MVC时,还有另一种方法可用来执行Ajax调用,这就是Ajax辅助方法. Ajax ...

  2. Eclispse 换主题、皮肤、配色,换黑色主题护眼

    链接地址:http://jingyan.baidu.com/article/6c67b1d68c03be2787bb1ed6.html Eclipse写android代码时,默认的文本和框架都是白色, ...

  3. 演练5-4:Contoso大学校园管理系统4

    在之前的教程中,我们已经完成了学校的数据模型.现在我们将读取和显示相关数据,请理解EF加载导航属性的方式. 一.Lazy.Eager.Explicit数据加载 使用EF为实体中的导航属性加载相关数据, ...

  4. StrPos,StrScan,

    Delphi提供的字符串函数里有一个Pos函数,它的定义是: function Pos(Substr: string; S: string): Integer; 它的作用是在字符串S中查找字符串Sub ...

  5. wince下GetManifestResourceStream得到的Stream是null的解决

    问题的引入 在编程过程中遇到下面这样一个问题: 有这样一个方法: public static AlphaImage CreateFromResource(string imageResourceNam ...

  6. shell登录模式及其相应配置文件(转)

    参考<linux命令.编辑器与shell编程>(清华大学出版社) 当启动shell时,它将运行启动文件来初始化自己.具体运行哪个文件取决于该shell是登陆shell还是非登陆shell的 ...

  7. 基于visual Studio2013解决C语言竞赛题之0808打印链表

     题目

  8. OFBIZ分享:利用Nginx +Memcached架设高性能的服务

    近年来利用Nginx和Memcached来提高站点的服务性能的作法,如一夜春风般的遍及大江南北,越来越多的门户站点和电子商务平台都採用它们来为自己的用户提供更好的服务体验.如:网易.淘宝.京东.凡客等 ...

  9. em换算px

    一般浏览器默认的1em=16px,所以常用字体大小如下: 10px=0.625em 12px=0.75em 14px=0.875em 16px=1em 18px=1.125em 20px=1.25em ...

  10. javascript笔记整理(window对象)

    浏览器对象模型 (BOM--Browser Object Model),window对象是BOM中所有对象的核心 A.属性 1.(位置类型-获得浏览器的位置) IE:window.screenLeft ...