當然,對於jQuery,匿名函數和延遲,我顯然缺少一些基本的東西。帶延遲的jQuery切換類只能工作一次
以下代碼僅在每頁加載時工作一次(它會添加類,然後在1秒後將其刪除,如果再次單擊,它將添加該類,但不會刪除類的持續時間頁面,除非我重新加載頁面):
var jElement = $(currElem);
jElement.addClass("highlight")
.delay(1000)
.queue(function(){
$(this).removeClass("highlight");
});
然而,
,如果我加入(不存在的)函數調用的參數,我把它在我的匿名函數,然後添加/刪除類組合將無限期地工作。
var jElement = $(currElem);
jElement.addClass("highlight")
.delay(1000)
.queue(function(randomFunction){
$(this).removeClass("highlight");
randomFunction(); //this makes it seemingly 'miraculously' work??
});
側面說明:
var jElement = $(currElem);
jElement.addClass("highlight")
.delay(1000)
.queue(function(randomFunction){
$(this).removeClass("highlight");
// this does NOT work; if I dont actually call the 'randomFunction'
// so that function, even though it does nothing; must somehow cause
// the implicit call of 'dequeue()' ??
});
請告訴我們究竟你正在努力實現與上面的代碼。 – Venemo 2013-02-14 20:48:35
我試圖模仿任何不包含有效信息的輸入字段的「臨時突出顯示」。 [用於表單驗證腳本]。 我注意到我的確切代碼工作,不管我傳入的函數是否被命名爲'next'。 有人可以解釋這種行爲嗎?它不足以傳遞函數,您還必須在匿名函數中調用它。我可以看到函數重載可能會導致這種隱式的'dequeue()'行爲,但觸發器本身存在於匿名函數中。 – 2013-02-14 21:11:32