不能從觸發器返回一個值,但你可以在許多不同的方式存儲信息,一種方法是使用一個對象作爲參數:
//event
$("element").on("click", function(event, informationObj) {
informationObj.userId = 2; //you have to access a propery so you can modify the original object
});
//trigger
var informationObj = {userId : 0};
$("element").trigger("click", [informationObj ]); //informationObj.userId === 2
另一種方法是使用jQuerys .data()
方法
//event
$("element").on("click", function() {
$(this).data("userId", 2);
});
//trigger
$("element").trigger("click").data("userId") //2
你能做的就是修改者的活動以外聲明的變量,然後調用觸發後使用它的另一件事,或將其存儲在具有與this
關鍵字這樣的事件的元素屬性:
//inside the event function
this.userId = 2;
//outside the event
$("element").trigger("click").get(0).userId
希望它能幫助。
編輯:
而且,看看下面@Arm0geddon answer,使用.triggerHandler()
,只是要小心,它有一定的副作用,如不冒泡的DOM層次結構。
的可能重複[使用jQuery .trigger調用返回一個值的自定義函數(http://stackoverflow.com/questions/3142724/use-jquery-trigger-to-call-a-custom-function-這-回報-A值) – 2014-07-10 09:16:41