从 jQuery 中提取 .live() 功能

发布于 2024-10-11 22:39:22 字数 496 浏览 4 评论 0原文

我正在编写一个 Firefox 插件,目前依赖于 jQuery 来完成以下操作:

  • 选择器
  • 动画
  • 一个特殊的 .live("focus") 冰雹玛丽事件捕获策略,恰好适用于 jQuery 1.4.2(但不适用于 1.4) .4)

jQuery 不太适合在 XUL 中运行,但我们能走到这一步已经是一个奇迹了。我们正在尝试删除 jQuery 要求,前两个很简单(动画很简单,我们可以使用 .querySelector() 而不是 jQuery),但是 .live 事实证明仅靠我们自己是不可能做到的。我尝试阅读源代码,但无法将其分开。

jQuery .live 函数在做什么?显然,除了 document.addEventListener("focus"/"focusin",function_to_pick_apart_events) 之外,还有更多的事情发生。这里还发生了什么?

I am writing a Firefox Add-On that currently is depending on jQuery for the following things:

  • Selectors
  • Animations
  • A special .live("focus") hail-mary event-catching manoeuvre that happens to work with jQuery 1.4.2 (but not 1.4.4)

jQuery isn't well suited for functioning inside XUL, and it's a miracle we've gotten this far with it. We're trying to remove the jQuery requirements, the first two are easy (animations are simple, and we can use .querySelector() instead of jQuery), but the .live has proven impossible to do on our own. I've tried reading the source code, but I haven't been able to piece it apart.

What is the jQuery .live function doing? There's clearly a lot more going on than document.addEventListener("focus"/"focusin",function_to_pick_apart_events). What else is going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜声 2024-10-18 22:39:22

正如其他人所说,在 Firefox 插件中使用 JQuery 可能并不像听起来那么无望。但直接回答你的问题:JQuery 是开源的,所以你可以随时自行查找。我自己对 JQuery 源代码并不熟悉,但我认为 https://github.com/jquery/jquery/blob/master/src/event.js#L552 可能是一个很好的起点......

As others have said, using JQuery in a Firefox addon may not be as hopeless as it sounds. But to answer your question directly: JQuery is open-source, so you can always look for yourself. I'm not familiar with the JQuery source code myself, but I think https://github.com/jquery/jquery/blob/master/src/event.js#L552 might be a good place to start...

浮华 2024-10-18 22:39:22

一种方法可能是在窗口级别捕获焦点事件,然后循环查找可能尚未具有焦点事件侦听器的感兴趣元素并添加一个。一个过于简单的示例:

<script>
function handleFocus(event) {
  event.target.value = "";
}
function captureFocus() {
  document.getElementById("target").onfocus = handleFocus;
}
window.onload = function() {
  addEventListener("focus", captureFocus, true);
}
</script>
<input id="target" value="Enter the value">

正如您所看到的,执行脚本时不需要输入存在于文档中,而是将事件侦听器附加到第一个焦点事件上。

One approach might be to capture focus events at the window level, then loop through looking for elements of interest that might not already have a focus event listener and add one. An over-simplified example:

<script>
function handleFocus(event) {
  event.target.value = "";
}
function captureFocus() {
  document.getElementById("target").onfocus = handleFocus;
}
window.onload = function() {
  addEventListener("focus", captureFocus, true);
}
</script>
<input id="target" value="Enter the value">

As you can see there is no requirement for the input to exist in the document when the script executes, it will attach the event listener on the first focus event instead.

知足的幸福 2024-10-18 22:39:22

您是否在 noConflict 模式下运行 JQuery?我有一些同事非常信赖 Firefox 附加组件中的 JQuery。我看过他们的工作,包括结果和代码,非常棒。我建议noConflict模式,并使用jQuery来引用JQuery而不是$符号。

.live 与 .bind 不同。事实上,live真的很精彩。它不仅将事件处理程序绑定到与特定 CSS 选择器匹配的元素,还会将处理程序绑定到导入到与该选择器匹配的页面上的任何新元素。显然必须在幕后进行某种监视才能继续将事件绑定到新元素,编辑:例如下面评论中描述的事件冒泡委托。

考虑到 XUL 的结构与 HTML DOM 不同,这可以解释为什么 .live 遇到问题。

一种可能的解决方法是使用 .bind,然后自己将处理程序添加到任何新元素(如果这确实是您使用 .live 的目标)。本质上,如果您在页面上添加与选择器匹配的新元素,则需要将任何事件绑定到那些在窗口加载时存在这些元素时会绑定的元素。

Are you running JQuery in noConflict mode? I have some colleagues that swear by JQuery in Firefox Add-ons. I've seen their work, both the result and the code, and it's fantastic. I suggest noConflict mode, and use jQuery to refer to JQuery instead of the $ symbol.

.live is different than .bind. In fact, live is pretty amazing. It not only binds event handlers to elements matching a specific CSS selector, but will also bind the handler to any new elements that are imported onto the page that match that selector. There clearly must be some kind of monitoring going on under the hood to continue to bind events to new elements, EDIT: such as the event bubbling delegation described in the comments below.

Considering XUL has a different structure than the HTML DOM, this could explain why you have trouble with .live.

A possible workaround could be to use .bind and then add handlers to any new elements yourself, if that is indeed your goal of using .live. Essentially, if you add new elements on the page that match the selector, you'll need to bind any events to those elements that would have been bound had those elements existed on window load.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文