jQuery元素存在于事件中

jQuery元素存在于事件中,jquery,events,exists,Jquery,Events,Exists,当页面上存在特定元素时,是否存在用于调用回调的事件或简单函数。 我不是问如何检查元素是否存在 例如 $("#item").exists(function(){ }); 我最终使用了ready事件 $("#item").ready(function(){ }); 看看.live函数。它在选择器中的所有当前和未来元素上执行 $('div').live(function(){}) jQuery插件似乎是大多数人用来解决这个问题的工具 Live Query通过绑定事件或事件来利用jQuery选择器

当页面上存在特定元素时,是否存在用于调用回调的事件或简单函数。 我不是问如何检查元素是否存在

例如

$("#item").exists(function(){ });
我最终使用了ready事件

 $("#item").ready(function(){ });

看看.live函数。它在选择器中的所有当前和未来元素上执行

$('div').live(function(){})

jQuery插件似乎是大多数人用来解决这个问题的工具

Live Query通过绑定事件或事件来利用jQuery选择器的功能 自动神奇地为匹配元素触发回调,即使在 页面已加载,DOM已更新

下面是一个快速的JSFIDLE,我将其放在一起演示:


下面是一个演示,演示每次创建一个div时都会触发一个事件,并写出刚刚创建的div的唯一id:

我遇到了同样的问题,所以我继续编写了一个插件:

$(选择器).waitUntilExists(函数)

代码:


ready函数似乎不是这样工作的@Drake。以这把小提琴为例:
(function ($) {

/**
* @function
* @property {object} jQuery plugin which runs handler function once specified element is inserted into the DOM
* @param {function} handler A function to execute at the time when the element is inserted
* @param {bool} shouldRunHandlerOnce Optional: if true, handler is unbound after its first invocation
* @example $(selector).waitUntilExists(function);
*/

$.fn.waitUntilExists    = function (handler, shouldRunHandlerOnce, isChild) {
    var found       = 'found';
    var $this       = $(this.selector);
    var $elements   = $this.not(function () { return $(this).data(found); }).each(handler).data(found, true);

    if (!isChild)
    {
        (window.waitUntilExists_Intervals = window.waitUntilExists_Intervals || {})[this.selector] =
            window.setInterval(function () { $this.waitUntilExists(handler, shouldRunHandlerOnce, true); }, 500)
        ;
    }
    else if (shouldRunHandlerOnce && $elements.length)
    {
        window.clearInterval(window.waitUntilExists_Intervals[this.selector]);
    }

    return $this;
}

}(jQuery));