我打電話的匿名函數:的JavaScript - 將參數傳遞給匿名函數
closeSidebar(function() {
alert("function called");
$(this).addClass("current");
setTimeout(function(){openSidebar()}, 300);
});
但$(this)
不能按預期工作,我需要把它作爲參數傳遞到函數。經過一番研究後,我認爲這會起作用:
closeSidebar(function(el) {
$(el).addClass("current");
setTimeout(function(){openSidebar()}, 300);
})(this);
但事實並非如此。如何將參數添加到匿名函數中?
jsFiddle - 點擊右邊的一個按鈕,它會動畫,然後調用上面的函數。當按鈕具有「當前」類時,它將在按鈕的左側有一個白色條,但該類不會改變。
啊支架在錯誤的地方..謝謝! –
這是有效的語法?在這裏,我想'closeSidebar(function(el){}(this));'最好*調用函數並將其返回給'closeSidebar'。 –