这样性能会更好。
使用链式操作
上面那个例子,我们可以写的更简洁一些:
?
|
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