在涉及到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()' ??
});