-
Book Overview & Buying
-
Table Of Contents
jQuery 1.3 with PHP
By :
The Javascript optimization tricks are not specific to jQuery, but are very good to know.
Chaining means running a number of functions on a selected element or group of elements.
Consider what happens when you do this:
$('.someclass').click(function(){
alert('clicked');
});
$('.someclass').css('background','red');
$('.someclass').text('some text'); On each line, jQuery is called with a fresh request to get all elements with the class name someclass and manipulate them in some way. This is quite expensive to do, as jQuery does not automatically cache the requests, so it's a fresh search every time.
To speed this up, you should chain the functions like this:
$('.someclass').click(function(){
alert('clicked');
})
.css('background','red')
.text('some text'); This runs the selector search once, and then runs all three operations on the same object.
Another thing to watch out for is the selector itself. Some types of selector run quicker...
Change the font size
Change margin width
Change background colour