More actions
No edit summary |
|||
Line 1: | Line 1: | ||
== JQuery Tips == | == JQuery Tips == | ||
'''1. Optimize performance of complex selectors''' | '''1. Optimize performance of complex selectors'''<br> | ||
var subset = $(""); | var subset = $(""); | ||
$("input[value^='']", subset); | $("input[value^='']", subset); | ||
<br> | <br> | ||
'''2. Set Context and improve the performance''' | '''2. Set Context and improve the performance'''<br> | ||
On the core jQuery function, specify the context parameter when. Specifying the context parameter allows jQuery to start from a deeper branch in the DOM, rather than from the DOM root. Given a large enough DOM, specifying the context parameter should translate to performance gains. | On the core jQuery function, specify the context parameter when. Specifying the context parameter allows jQuery to start from a deeper branch in the DOM, rather than from the DOM root. Given a large enough DOM, specifying the context parameter should translate to performance gains. | ||
$("input:radio", document.forms[0]); | $("input:radio", document.forms[0]); | ||
<br> | <br> | ||
'''3. Live Event Handlers''' | '''3. Live Event Handlers'''<br> | ||
Set an event handler for any element that matches a selector, even if it gets added to the DOM after the initial page load: | Set an event handler for any element that matches a selector, even if it gets added to the DOM after the initial page load: | ||
$('button.someClass').live('click', someFunction); | $('button.someClass').live('click', someFunction); | ||
<br> | <br> | ||
'''4. To show / hide element''' | '''4. To show / hide element'''<br> | ||
$("a").hide(); | $("a").hide(); | ||
$("a").show(); | $("a").show(); | ||
'''5. Check element exists'''<br> | |||
if ($("#someDiv").length) { | |||
} | |||
<br> | <br> |
Revision as of 02:43, 29 April 2010
JQuery Tips
1. Optimize performance of complex selectors
var subset = $("");
$("input[value^=]", subset);
2. Set Context and improve the performance
On the core jQuery function, specify the context parameter when. Specifying the context parameter allows jQuery to start from a deeper branch in the DOM, rather than from the DOM root. Given a large enough DOM, specifying the context parameter should translate to performance gains.
$("input:radio", document.forms[0]);
3. Live Event Handlers
Set an event handler for any element that matches a selector, even if it gets added to the DOM after the initial page load:
$('button.someClass').live('click', someFunction);
4. To show / hide element
$("a").hide();
$("a").show();
5. Check element exists
if ($("#someDiv").length) {
}