jQuery will call the function only if there is a match, no need to double check. It should be well known, but obviously it is not. Unnecessary DOM traversal is a expensive operation, avoid it when possible. Using jQuery's each and map is more reliable because they won't break if another library is extending the Array object. Events can be namespaced The data method also accept namespaces Instead of You can do triggerHandler is also useful for creating custom events, which leads me to my next tip In some situation it can saves you lots of pain, it's also really handy to encapsulate plugins interactions. Let me illustrate. jQuery comes with a nice unit test framework called QUnit. Writing tests is quite easy and allow you to confidently modify your code while ensuring that it still works as expected. 1. Be lazy
// Don't
if ($('#item').get(0)) {
$('#item').someFunction();
}
// Or
if ($('#item').length) {
$('#item').someFunction();
}
// Just do
$('#item').someFunction();
2. Use shortcuts
// You can but..
$(document).ready(function(){
// ...
});
// There is a shorter equivalent
$(function(){
// ...
});
3. Chain
// Don't
$('#frame').fadeIn();
$('#frame .title').show();
$('#frame a:visited').hide;
// Do
$('#frame').fadeIn()
.find('.title').show().end()
.find('a:visited').hide();
4. Group queries
// Ugly
$('div.close').click(closeCallback);
$('button.close').click(closeCallback);
$('input.close').click(closeCallback);
// Not ugly
$('div.close, button.close, input.close')
.click(closeCallback);
5. Select siblings like a pro
// Don't
$('#nav li').click(function(){
$('#nav li').removeClass('active');
$(this).addClass('active');
});
// Do
$('#nav li').click(function(){
$(this).addClass('active')
.siblings().removeClass('active');
});
6. Use each and map
// Try to avoid
var output = [];
for (var i=0;i < arr.length; i++) {
output.push(arr[i]);
}
// Do
var output = $.map(arr, function() {
...
});
// Or
var output = [];
$.each(arr, function(index, value) {
output.push(value);
});
7. Use namespaces
$('input').bind('blur.validation', function(e){
// ...
});
$('input').data('validation.isValid', true);
8. triggerHandler is great
var refreshFrame = function() {
$('#frame').load('http://reddit.com');
};
$('.button').click(refreshFrame);
refreshFrame();
$('.button').click(function() {
$('#frame').load('/page/frame.html');
}).triggerHandler('click');
// You can also use a shortcut
$('.button').click(function() {
$('#frame').load('/page/frame.html');
}).click();
9. Custom events
$('.button').click(function() {
$('div#frame').load('/page/frame.html', function(){
$(this).triggerHandler('frameLoaded');
});
});
// PluginA.js
$('#frame').bind('frameLoaded', function(){
$(this).show('slide', {direction: 'top'});
});
// PluginB.js
$('div').bind('frameLoaded', function(){
// do something else
});
10. Test !
module("A simple test");
test("The frame should appear #button is clicked", function() {
expect(1);
$('#button').click();
ok($('#frame').is(':visible'), "The frame is visible" );
});
| 10個超好用的撰寫jQuery技巧 |
2009-06-01
人氣:1087
人氣:1087
[我要留言]
|
|
|
|
|
isaPek jmybpzknxias
|
|
|
It was dark when I woke. This is a ray of suinhsne.
|
|
|
I love this site, it is really nice.
-------------------------- JN0-100 dumps NS0-153 dumps NS0-163 dumps PMI-002 dumps |
留言
分類:無
標籤:無
轉貼:
