一旦頁面上存在特定元素,是否存在用於調用回調的事件或簡單函數。 我不問如何檢查一個元素是否存在。jQuery元素存在事件
爲例
$("#item").exists(function(){ });
我結束了使用的準備情況
$("#item").ready(function(){ });
一旦頁面上存在特定元素,是否存在用於調用回調的事件或簡單函數。 我不問如何檢查一個元素是否存在。jQuery元素存在事件
爲例
$("#item").exists(function(){ });
我結束了使用的準備情況
$("#item").ready(function(){ });
看看在.live功能。它在選擇器中的所有當前和將來的元素上執行。 ('div')。live(function(){});
LiveQuery jQuery插件似乎是大多數人用來解決這個問題。
實況查詢通過結合事件或 射擊回調匹配的元素自動神奇,即使在 頁面已經被加載和DOM更新利用jQuery選擇的權力。
這裏有一個快速的jsfiddle,我放在一起,以證明這一點:http://jsfiddle.net/87WZ3/1/
這裏的每創建一個div時間觸發一個事件,並寫出剛創建的div的唯一ID的演示: http://jsfiddle.net/87WZ3/2/
這正是我正在尋找的! THX – user357034
我有同樣的問題,所以我繼續寫了這一個插件:https://gist.github.com/4200601
$(selector).waitUntilExists(function);
代碼:
(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));
這很好 - 謝謝瑞恩! – Michael
那你想幹什麼? –
ready函數似乎不能以這種方式工作@Drake。看這個小提琴爲例:http://jsfiddle.net/YDn5f/ –