Test

Page 172

Example 5:

Can bind custom events too.

Javascript

$("body").delegate("p", "myCustomEvent", function(e, myName, myValue){ $(this).text("Hi there!"); $("span").stop().css("opacity", 1) .text("myName = " + myName) .fadeIn(30).fadeOut(1000); }); $("button").click(function () { $("p").trigger("myCustomEvent"); });

CSS p { color:red; } span { color:blue; }

HTML <p>Has an attached custom event.</p> <button>Trigger custom event</button> <span style="display:none;"></span>

jQuery.proxy Takes a function and returns a new one that will always have a particular context. jQuery.proxy(context, name):Function context:Object name:String

Added in version 1.4

The object to which the context of the function should be set. The name of the function whose context will be changed (should be a property of the context object).

This method is most useful for attaching event handlers to an element where the context is pointing back to a different object. Additionally, jQuery makes sure that even if you bind the function returned from jQuery.proxy() it will still unbind the correct function if passed the original. Example 1:

Change the context of functions bound to a click handler using the "function, context" signature. Unbind the first handler after first

click. HTML <p><button type="button" id="test">Test</button></p> <div id="log"></div>


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.