How to make live custom events in jQuery(如何在 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
和 focusout
事件作为 focus
和 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 事件.两个例外:因为 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 中制作实时自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 jQuery 中制作实时自定义事件
基础教程推荐
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01