這樣性能會更好。
使用鏈式操作
上面那個例子,我們可以寫的更簡潔一些:
?
1
2
|
var $loading = $( '#loading' ); $loading.html( '完畢' ).fadeOut(); |
這樣是不是更省力氣書寫呢? 但是注意鏈式操作不要串的過多了,如果太多了,對于你自己的debug的眼力是一個巨大的挑戰
精簡jQuery代碼
盡量把一些代碼都整合到一起,請勿這樣編碼:
?
1
2
3
4
5
|
// ??!反面人物$button.click(function(){ $target.css( 'width' , '50%' ); $target.css( 'border' , '1px solid #202020' ); $target.css( 'color' , '#fff' ); }); |
應該這樣書寫:
?
1
2
3
|
$button.click( function (){ $target.css({ 'width' : '50%' , 'border' : '1px solid #202020' , 'color' : '#fff' }); }); |
避免使用全局類型的選擇器
請勿如下方式書寫:
?
1
|
$( '.something > *' ); |
原文轉自:http://www.admin10000.com/document/3968.html