PDA

View Full Version : مفهوم tigger وtriggerHandler و در جی کوئری



daneshh
دوشنبه 10 بهمن 1390, 01:40 صبح
با سلام و خسته نباشید
لطفا در مورد مفهوم tigger وtriggerHandler و در جی کوئری توضیحی بنویسید نمی توانم بفهم کار این دو تا در جی کوئری چیست؟
با تشکر

emad_67
چهارشنبه 12 بهمن 1390, 12:13 عصر
مهمترین تفاوتشون اینه که triggerHandler رویداد پیش فرض اون المان رو fire نمی کنه و فقط event ای رو اجرا می کنه که به خود المان bind شده.
به طور مثال رفتار پیش فرض رویداد focus در بروزر باعث میشه که فوکوس بر روی اون المان قرار بگیره حالا با این فرض کد زیر رو در نظر بگیر:



<button id="old">.trigger("focus")</button>
<button id="new">.triggerHandler("focus")</button><br/><br/>

<input type="text" value="To Be Focused"/>



$("#old").click(function(){
$("input").trigger("focus");
});
$("#new").click(function(){
$("input").triggerHandler("focus");
});
$("input").focus(function(){
$("<span>Focused!</span>").appendTo("body").fadeOut(1000);
});


می بینی که وقتی focus توسط trigger اجرا میشه، علاوه بر رویدادی که به صورت دستی به متد focus توسط خود jquery بایند شده، کرسر هم بر روی texbox قرار میگیره یعنی رفتار پیش فرض focus هم اجرا شده، اما در حالت triggerHandler فقط همون مدتی فراخوانی میشه که bind شده و نه رفتار پیش فرض.

تفاوت های دیگه هم دارن که می تونی تو سایت خود jquery بخونی:



The .triggerHandler() method behaves similarly to .trigger(), with the following exceptions:


The .triggerHandler() method does not cause the default behavior of an event to occur (such as a form submission).
While .trigger() will operate on all elements matched by the jQuery object, .triggerHandler() only affects the first matched element.
Events created with .triggerHandler() do not bubble up the DOM hierarchy; if they are not handled by the target element directly, they do nothing.
Instead of returning the jQuery object (to allow chaining), .triggerHandler() returns whatever value was returned by the last handler it caused to be executed. If no handlers are triggered, it returns undefined

http://api.jquery.com/triggerHandler/