简体   繁体   English

如果调用Javascript事件侦听器并且缺少目标元素,会发生什么情况?

[英]What happens if a Javascript event-listener is called and target element is missing?

For the moment, we're loading site-wide event-listeners from a single common.js file for a Rails project. 目前,我们正在从Rails项目的一个common.js文件加载整个站点范围的事件监听器。 We're aware of (most of) the tradeoffs involved there, and are just trying to mitigate them. 我们知道这里涉及的(大多数)折衷,只是在试图减轻它们。 Once our basic architecture takes hold, we may move them off to separate files by controller or by view. 一旦掌握了基本架构,我们就可以通过控制器或视图将它们移至单独的文件中。

For the moment, the quick question is how we can activate them only when necessary, which begs the mangled, pseudo-zen question: 目前,一个快速的问题是我们如何仅在必要时才能激活它们,这困扰着伪造的伪禅问题:

if an event-listener is declared in a forest when nobody is around to hear it, does it still make a sound? 如果某个事件侦听器在森林中被宣布而没有人在周围听到,那么它还会发出声音吗?

In other words, if one declares a basic listener (ie., nothing persistent like .live() or .delegate() ) in the javascript for a given page, and the target element is not actually present on that given page, does anything really happen, other than the few cycles devoted to evaluating it and checking the DOM for the element? 换句话说,如果给定页面的javascript中声明了基本的侦听器(即,没有像.live().delegate()这样的持久性.delegate() ,并且目标元素实际上不在该给定页面上,则执行任何操作除了用于评估它并检查元素的DOM的几个周期外,真的发生了吗? Is it active in memory, looking for that element? 它在内存中活跃吗,正在寻找那个元素? Something else? 还有吗 It never seems to throw an error, which is interesting, given that in other contexts a call like that would generate a null/nil/invalid type of error. 考虑到在其他情况下这样的调用将生成null / nil / invalid类型的错误,似乎永远不会引发错误,这很有趣。

For instance: 例如:

  $(document).ready(function () {
      $('#element').bind('blur keyup', function);
    }

Assume that #element isn't present. 假设#element不存在。 Does anything really happen? 真的有事吗? Moreover is it any better to wrap it in a pre-filter like: 此外,将其包装在类似以下的预过滤器中是否更好:

  $(document).ready(function () {
    if ($('#element')) {
      $('#element').bind('blur keyup', function);
    }

Or, are the .js interpreters in the browsers smart enough to simply ignore a basic listener declared on an element that's not present at $(document).ready ? 或者,浏览器中的.js解释器是否足够聪明,以至于可以忽略在$(document).ready上不存在的元素上声明的基本侦听器? Should we just declare the initial, simple form above, and leave it at that, or will checking for the element first somehow save us a few precious resources and/or avoid some hidden errors we're not seeing? 我们应该只在上面声明初始的简单形式,然后保留它,还是先检查元素以某种方式为我们节省一些宝贵的资源和/或避免一些我们没有看到的隐藏错误? Or is there another angle I'm missing? 还是我想念另一个角度?

Thanks! 谢谢!

JQuery was designed to work with 0+ selected elements. JQuery旨在与0+个所选元素一起使用。
If no elements were selected, nothing will happen. 如果未选择任何元素,则不会发生任何事情。

Note that you will never get null when using jQuery selector. 请注意,使用jQuery选择器时永远不会为空。 For example: 例如:

$('#IDontExist') // != null
$('#IDontExist').length === 0 // true (it's ajQuery object with 
                              //       zero selected elements).

The docs says: 文档说:

If no elements match the provided selector, the new jQuery object is "empty"; 如果没有元素与提供的选择器匹配,则新的jQuery对象为“空”;否则为0。 that is, it contains no elements and has .length property of 0. 也就是说,它不包含任何元素,并且.length属性为0。

$('#element') if results into empty set then jQuery will not do anything. $('#element')如果结果为空集,则jQuery将不执行任何操作。

Since jQuery always returns an object we can can call the methods on an empty set also but internally it will do the checking before applying it's logic. 由于jQuery总是返回对象,因此我们也可以在空集上调用方法,但是在内部,它将在应用逻辑之前进行检查。

Even if you want to check if the element exists before attaching the event handler you can use length property of jQuery object. 即使您要在附加事件处理程序之前检查元素是否存在,也可以使用jQuery对象的length属性。

if ($('#element').length > 0) {
    $('#element').bind('blur keyup', function);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript如何用事件监听器移动变量? - Javascript how to move variables with event-listener? Next.js:将“样式”添加到事件侦听器中的元素 - Next.js: Add 'style' to element in event-listener 在JavaScript中触发单个事件监听器的多个动作(单击) - Triggering multiple actions for single event-listener(on-click) in javascript 有没有办法在 Javascript 中获取所有事件侦听器绑定? - Is there a way to get all event-listener bindings in Javascript? Javascript(Dojo):销毁对象时是否删除事件监听器 - Javascript (Dojo): Is event-listener removed when object is destroyed 按钮事件侦听器无响应 - Button Event-listener is unresponsive 如果我将 JavaScript 事件绑定到一个元素,然后删除该元素,该事件会发生什么? - If I bind a JavaScript event to an element, then delete the element, what happens to the event? 关于问答游戏中的事件监听器 - About an event-listener inside a quiz game Javascript侦听器在事件目标的子对象而非实际元素上触发 - Javascript listener triggers on child of the event target, not the actual element iOS Safari:“ mousemove”上有事件监听器时不会触发“ click”事件 - iOS Safari: 'click' event not triggerd when there is an event-listener on 'mousemove'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM