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

查看:18
本文介绍了如何在 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 事件(包括自定义事件),并且引入了 focusinfocusout 事件作为 focusblur.

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 事件.两个例外:因为 focus 和 blur 实际上并不是冒泡事件,所以我们需要使用 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天全站免登陆