翻译自http://brandonaaron.net/blog/2009/03/26/special-events 原作者:Brandon Aaron jQuery自1.2.2版开始引入称为"特别事件"的扩展API。These events are special because they have the ability to do some extra work for specific events and even the option to bypass some of the internal jQuery event system. 有了这些特别事件你可以创建需要一些setup work的自定义事件,甚至你可以完全重载标准jQuery事件的行为。 We use special events in jQuery to create the?“mouseenter” and “mouseleave” events. In addition to those two events we used it to make the?“ready” event?and I use it to normalize the “mousewheel” event in the?mouse wheel plugin. 我们打算建立一个新事件,该事件当用户对某个元素点击三次以后触发。传统上我们应该建立类似jQuery.fn.tripleclick这样的jQuery插件. 但在这里我们不这么做, 我们想要利用标准jQuery事件系统的bind语法及其它好处比如event normalization, data, and namespaces. 首先我们要建立这个特殊事件。每个特殊事件需要定义 In jQuery 1.3 there is a new special event type called “Special All” that operates for all handlers but has a slightly different behavior. However, that is for another?article.(这个特性已经被取消,不应该再使用Special All了,可以改用add/remove钩子,参见最后的延伸阅读部分) 我们的“tripleclick”插件大概骨架如下: Notice that the? Behind the scenes we are actually going to utilize the native “click” event to keep track of the number of clicks on an element. We’ll also need a handler that actually does the heavy work of keeping track. I’m going to add a third method called? The updated? To quickly break down the handler code. First we get the number of clicks via the data?API?and increment the number by 1. Then we check to see if it has been clicked 3 times. If so, we then need to reset the number of clicks and trigger the other event handlers as the comment indicates. Finally, we store the new value for the number clicks on the element via the data?API. The handler has to set the event type to “tripleclick” because behind the scenes we actually use a click event. jQuery uses the event type to know which handlers it should call and we want it to call the event handlers for our “tripleclick”?event. We can now use our special event just like we’d use any other event via the? You can see an example of this new special event in action?here. You could enhance this event by requiring all three clicks to be within a certain timeframe. You could achieve this by also storing the? 前面我说过特别事件能够bypass到内部的jQuery事件系统。The functionality that can be skipped is the actual binding of the event to the element using the? ? jQuery 1.4为特别事件新增的add和remove钩子:?http://brandonaaron.net/blog/2009/06/4/jquery-edge-new-special-event-hooks Automating with Special Events:?http://brandonaaron.net/blog/2009/06/17/automating-with-special-events Special Events: The changes in jQuery 1.4.2:?http://brandonaaron.net/blog/2010/02/25/special-events-the-changes-in-1-4-2 ?特别事件
一个例子:?“tripleclick”三次点击
setup
和teardown
方法。方法setup在事件被bind时调用而
方法teardown则在解除bind时调用
。注意:这两个方法对每个元素只会被调用一次,因为jQuery事件系统会管理多次bind的事件处理器等所有繁琐的事情。jQuery.event.special.tripleclick = {
setup: function(data, namespaces) {
var elem = this;
},
teardown: function(namespaces) {
var elem = this;
}
};
setup
?event gets passed the data and namespaces that were used when binding the event. Also that theteardown
?event gets passed the namespaces that were used when unbinding the event. Although, they are only marginally useful here since this is the data and namespaces associated with the very first event handler bound to a particular element. But you could use the data to configure the event for all the handlers of that type for an element. The scope, or the value ofthis
, for these two methods is the element the event is being bound?to.handler
to the?tripleclick
?special event. To make the code a little more simple I’m going to track the number of clicks on an element by using the?jQuery data?API.tripleclick
?special event looks like?this.jQuery.event.special.tripleclick = {
setup: function(data, namespaces) {
var elem = this, $elem = jQuery(elem);
$elem.bind('click', jQuery.event.special.tripleclick.handler);
},
teardown: function(namespaces) {
var elem = this, $elem = jQuery(elem);
$elem.unbind('click', jQuery.event.special.tripleclick.handler);
},
handler: function(event) {
var elem = this, $elem = jQuery(elem), clicks = $elem.data('clicks') || 0;
clicks += 1;
if ( clicks === 3 ) {
clicks = 0;
// set event type to "tripleclick"
event.type = "tripleclick";
// let jQuery handle the triggering of "tripleclick" event handlers
jQuery.event.handle.apply(this, arguments)
}
$elem.data('clicks', clicks);
}
};
The?Example
bind
?method. For example to bind a “tripleclick” event to all divs we’d write the following?code.jQuery('div').bind('tripleclick', function(event) {
alert('triple clicked');
});
event.timeStamp
?property of the previous click and comparing the distance between it and the latest?click.返回值
addEventListener
?or?attachEvent
?methods. This functionality is skipped based on the return value. 返回任何非false的值
会阻止jQuery将该事件通过DOM实际bind到元素上。换句话说,如果从方法setup和teardown中
return false的话,jQuery将调用DOM API将事件bind到元素上。在我们的tripleclick插件中,我们不希望在元素上bind这个"tripleclick"事件(并不存在这样的DOM事件),所以我们没有返回任何值(就是说,返回了undefined)
。延伸阅读
详细解决方案
jQuery的"不一般事件"扩展
热度:340 发布时间:2012-09-03 09:48:39.0
相关解决方案
- jquery 获取jsp页面的id解决方法
- struts2 对象属性流入不进去, 报错:target is null for setProperty(null, "x" [Ljava.lang.Stri
- request.setAttribute("list" "''");该怎么解决
- Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml解决办法
- struts2标签 <s:if test="表达式">表达式的写法解决方案
- xml文件不能被准确解析/The processing instruction target matching "[xX][mM][lL]" is not al
- sql中获取d天后的日期,格式为"yyyyMMdd"怎么做到
- spring @Scope("prototype")注解更新有关问题,寻求帮助
- 怎么在eclipse的JSP裡在使用JS語法時可以直接""後出現方法
- 关于request.getParamater("name")若干疑点,html5新特性placeholder
- java.lang.NumberFormatException: For input string: "id"该如何处理
- jquery,二维数组取值。解决办法
- 怎么取<s:property value="news.CONTENT" escape="false"/>内容前几位
- 为什么Ext.getCmp("panelID").collapsed = false;无效呢
- 求jquery.form.js+jquery.validate.min.js 用ajax提交表单的代码范例
- jquery 提交form表单不用插件的那种 如何提交
- 新人第一帖!JSF有关问题:<h:inputText value="{user.name}"/>
- request.setAttribute("message" message)不能传int参数,该怎么处理
- 关于 if(rs.getString("").trim().equals(""))解决办法
- jquery ajax回传没有值,该怎么处理
- <base href="<%=basePath%>"> 有关问题
- jquery easyUI datagrid struts2有关问题
- jquery form 有关问题
- form 表单交付 <form action="<c:url value='desktop'/>"
- springmvc jquery ajax 提交复杂对象,415异常解决办法
- java报错Syntax error on token "return" invalid Type解决方案
- 应用Struts2 与 jquery,ajax验证用户注册,不用从数据库获取数据
- jquery ajax select解决方法
- ENCTYPE="multipart/form-data"文件下传有关问题
- request.setAttribute("message" message)不能传int参数,该怎么解决