缓存变量
1 // 糟糕 2 h = $(‘#element‘).height(); 3 $(‘#element‘).css(‘height‘,h-20); 4 5 // 建议 6 $element = $(‘#element‘); 7 h = $element.height(); 8 $element.css(‘height‘,h-20);
避免全局变量
1 // 糟糕 2 $element = $(‘#element‘); 3 h = $element.height(); 4 $element.css(‘height‘,h-20); 5 6 // 建议 7 var $element = $(‘#element‘); 8 var h = $element.height(); 9 $element.css(‘height‘,h-20);
使用匈牙利命名法
1 // 糟糕 2 var first = $(‘#first‘); 3 var second = $(‘#second‘); 4 var value = $first.val(); 5 6 // 建议 - 在jQuery对象前加$前缀 7 var $first = $(‘#first‘); 8 var $second = $(‘#second‘), 9 var value = $first.val();
使用 Var 链(单 Var 模式)
1 var 2 $first = $(‘#first‘), 3 $second = $(‘#second‘), 4 value = $first.val(), 5 k = 3, 6 cookiestring = ‘SOMECOOKIESPLEASE‘, 7 i, 8 j, 9 myArray = {};
请使用‘On’
1 // 糟糕 2 $first.click(function(){ 3 $first.css(‘border‘,‘1px solid red‘); 4 $first.css(‘color‘,‘blue‘); 5 }); 6 7 $first.hover(function(){ 8 $first.css(‘border‘,‘1px solid red‘); 9 }) 10 11 // 建议 12 $first.on(‘click‘,function(){ 13 $first.css(‘border‘,‘1px solid red‘); 14 $first.css(‘color‘,‘blue‘); 15 }) 16 17 $first.on(‘hover‘,function(){ 18 $first.css(‘border‘,‘1px solid red‘); 19 })
精简javascript
1 // 糟糕 2 $first.click(function(){ 3 $first.css(‘border‘,‘1px solid red‘); 4 $first.css(‘color‘,‘blue‘); 5 }); 6 7 // 建议 8 $first.on(‘click‘,function(){ 9 $first.css({ 10 ‘border‘:‘1px solid red‘, 11 ‘color‘:‘blue‘ 12 }); 13 });
链式操作
1 // 糟糕 2 $second.html(value); 3 $second.on(‘click‘,function(){ 4 alert(‘hello everybody‘); 5 }); 6 $second.fadeIn(‘slow‘); 7 $second.animate({height:‘120px‘},500); 8 9 // 建议 10 $second.html(value); 11 $second.on(‘click‘,function(){ 12 alert(‘hello everybody‘); 13 }).fadeIn(‘slow‘).animate({height:‘120px‘},500);
维持代码的可读性
1 // 糟糕 2 $second.html(value); 3 $second.on(‘click‘,function(){ 4 alert(‘hello everybody‘); 5 }).fadeIn(‘slow‘).animate({height:‘120px‘},500); 6 7 // 建议 8 $second.html(value); 9 $second 10 .on(‘click‘,function(){ alert(‘hello everybody‘);}) 11 .fadeIn(‘slow‘) 12 .animate({height:‘120px‘},500);
选择短路求值
1 // 糟糕 2 function initVar($myVar) { 3 if(!$myVar) { 4 $myVar = $(‘#selector‘); 5 } 6 } 7 8 // 建议 9 function initVar($myVar) { 10 $myVar = $myVar || $(‘#selector‘); 11 }
选择捷径
1 // 糟糕 2 if(collection.length > 0){..} 3 4 // 建议 5 if(collection.length){..}
繁重的操作中分离元素
1 // 糟糕 2 var 3 $container = $("#container"), 4 $containerLi = $("#container li"), 5 $element = null; 6 $element = $containerLi.first(); 7 //... 许多复杂的操作 8 9 // better 10 var 11 $container = $("#container"), 12 $containerLi = $container.find("li"), 13 $element = null; 14 $element = $containerLi.first().detach(); 15 //... 许多复杂的操作 16 $container.append($element);
熟记技巧
1 // 糟糕 2 $(‘#id‘).data(key,value); 3 4 // 建议 (高效) 5 $.data(‘#id‘,key,value);
使用子查询缓存的父元素
1 // 糟糕 2 var 3 $container = $(‘#container‘), 4 $containerLi = $(‘#container li‘), 5 $containerLiSpan = $(‘#container li span‘); 6 7 // 建议 (高效) 8 var 9 $container = $(‘#container ‘), 10 $containerLi = $container.find(‘li‘), 11 $containerLiSpan= $containerLi.find(‘span‘);
避免通用选择符
1 // 糟糕 2 $(‘.container > *‘); 3 4 // 建议 5 $(‘.container‘).children();
避免隐式通用选择符
1 // 糟糕 2 $(‘.someclass :radio‘); 3 4 // 建议 5 $(‘.someclass input:radio‘);
优化选择符
// 糟糕 $(‘div#myid‘); $(‘div#footer a.myLink‘); // 建议 $(‘#myid‘); $(‘#footer .myLink‘);
避免多个ID选择符
1 // 糟糕 2 $(‘#outer #inner‘); 3 4 // 建议 5 $(‘#inner‘);
坚持最新版本
1 // 糟糕 - live 已经废弃 2 $(‘#stuff‘).live(‘click‘, function() { 3 console.log(‘hooray‘); 4 }); 5 6 // 建议 7 $(‘#stuff‘).on(‘click‘, function() { 8 console.log(‘hooray‘); 9 }); 10 // 注:此处可能不当,应为live能实现实时绑定,delegate或许更合适
必要时组合jQuery和javascript原生代码
最后忠告
原文:http://www.cnblogs.com/gangerdai/p/6427632.html