如何在jQuery中实现自定义事件 [英] How to make live custom events in jQuery

查看:87
本文介绍了如何在jQuery中实现自定义事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery有一个非常方便的事件绑定器,称为live(),它会将事件即时添加到DOM元素(即使稍后添加到DOM中的元素)。问题在于,它仅适用于特定事件(在此列出的文档)。

jQuery has a really handy event binder called live() which will add events to DOM elements on the fly (even for the elements that will be added later to the DOM). The problem is that it's only working on specific events (listed here in documentation).

我真的想有现场直播活动的焦点,模糊和变化,现在不支持。此外,如果我可以进行现场自定义的活动,这将是我的应用程序的大改变游戏。我现在的大多数代码都致力于重新绑定旧事件(更改,重点和自定义事件以使项目可拖动或可调整大小)到通过ajax添加的新dom元素。

I really want to have live events for focus,blur and change which is not supported by live right now. Besides, if I can make live custom events, it will be big game changer for my app. Most of the code that I have right now is dedicated to rebinding old events (change, focus, and custom events for making items draggable or resizable) to new dom elements that have been added through ajax.

任何想法?我想事件委托是要走的路,但我现在就可以使代码更加复杂。也许是一个处理事件代理的插件...不确定。帮助我找到一个解决方案。

Any idea? I guess event delegation is the way to go, but I right now it'll make the code more complicated. Maybe a plugin that handle event delegations... not sure. Help me find a solution.

推荐答案

这个功能现在在jQuery 1.4中可用。 live()现在支持所有JavaScript事件(包括自定义事件)和 focusin 焦点事件已被引入为焦点 blur 的冒泡版本。

This functionality is now available in jQuery 1.4. live() now supports all JavaScript events (including custom events), and the focusin and focusout events have been introduced as bubbling versions of focus and blur.

jQuery 1.4文档.live()


从jQuery 1.4开始,.live()方法支持自定义事件以及所有JavaScript事件。两个例外:由于焦点和模糊实际上并不是冒泡事件,所以我们需要使用focusin和focusout。

As of jQuery 1.4, the .live() method supports custom events as well as all JavaScript events. Two exceptions: Since focus and blur aren't actually bubbling events, we need to use focusin and focusout instead.

这篇关于如何在jQuery中实现自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆