Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL
Help

Summary > Summary - Pg. 949

CHAPTER 33 USING THE JQUERY UTILITY METHODS Tip This method only works on HTMLElement objects. If you want to perform the same check on jQuery objects, consider using the find method, which I describe in Chapter 6. Creating a Proxy Function The proxy method allows you to create a function whose this variable is always an object that you specify. Listing 33-16 shows how the proxy method can be used. Listing 33-16. Using the proxy method ... <script type="text/javascript"> $(document).ready(function() { $('img').hover($.proxy(handleMouse, $('img').eq(0))); function handleMouse(e) { $(this).css("border", e.type == "mouseenter" ? "thick solid red" : ""); } }); </script> ... This example contains a function called handleMouse that sets the border on the element represented by the this variable in response to mouse events. I used the proxy function to create a wrapper around the handleMouse function, which ensures that the value of this is always set to the first img element in the document. The effect of using the proxy method is that regardless of which img element triggers the mouse event, the border is always applied to the first img element. Tip This method is not widely used--not least because only the this variable is changed. As a consequence, the proxy method has no real effect if you are in the habit of using the Event.target property to find out which element triggered the event, for example. Summary In this chapter, I described the jQuery utility methods--an eclectic set of helpful functions that can be used to perform advanced operations on jQuery objects or that supplement the JavaScript language features to provide support that programmers commonly need. These are the kinds of methods you are glad exist when you need them, but you can safely forget about them for most web application projects. 919