Last active
December 13, 2015 20:58
-
-
Save Sljubura/4973780 to your computer and use it in GitHub Desktop.
Some jQuery tips.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Always use lates version | |
// Not all selectors are equaly fast | |
// Fastest to slowest selectors. | |
$('#selector-by-id'); | |
$('div', 'input'); | |
$('.class-selectors'); | |
// And the slowest are pseudo selectors | |
$(':visible, :hidden, [attribute=value]'); | |
// Efficient selectors | |
$('#wrapper').find('.first'); // Better | |
$('#wrapper .content .main article.first'); | |
// Use caching! | |
var $firstArticle = $('#wrapper').find('.first'); | |
// But avoid using global variables by using immediate function or similar. | |
// Use chaining | |
$firstArticle | |
.fadeIn() | |
.addClass('active') | |
.css({ // Also, use object notation for adding multiple css rules | |
paddingLeft: '20px', // instead of chaining .css() multiple times. | |
marginLeft: '20px' | |
}); | |
// Attaching data | |
$('#node').data(key, value); // Slower, creates jQuery object | |
$.data('#node', key, value); // Faster |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment