Dojo入门:dojo中的事件处理
转载自:http://www.cnblogs.com/youring2/archive/2012/06/10/2544168.htmlJS为DOM添加事件在原生的环境下,为DOM添加事件处理函数有多种方法: <inputtype="button"name="btn"value="点击…"id="btn"onclick="btnClick"/> 或者使用以下方法: <inputtype="button"name="btn"value="点击…"id="btn"/> <scripttype="text/javascript"> functionbtnClick(){ alert("我被点击了"); } document.getElementById("btn").onclick=btnClick;</script> 以上这两种方法存在明显的弊端:每一个事件只能指定一个事件处理函数,另外,如果要移除一个事件处理函数,似乎只能使用: document.getElementById("btn").onclick=null; 或者 document.getElementById("btn").onclick=""; 这种方式极不利于模块化编程。W3C DOM Level2 标准有了新的事件模型,新的事件模型允许为事件添加多个处理方法,并加入了事件冒泡机制。 使用新的事件模型来添加事件处理方法: <inputtype="button"name="btn"value="点击…"id="btn"/> <scripttype="text/javascript"> functionbtnClick(){ alert("我被点击了"); } document.getElementById("btn").addEventListener("click",btnClick);</script> 移除一个事件处理方法: document.getElementById("btn").removeEventListener("click",btnClick); dojo事件处理dojo使用connect方法为DOM添加事件处理函数。 <scripttype="text/javascript"> functionbtnClick(){ alert("我被点击了"); }varbtn=dojo.byId("btn"); dojo.connect(btn,"onclick",255);">dojo.connect方法的官方解释:dojo.connect is the core event handling and delegation method in Dojo. It allows one function to "listen in" on the execution of any other,triggering the second whenever the first is called. Many listeners may be attached to a function,and source functions may be either regular function calls or DOM events. |