加载中…
个人资料
real小辉侠
real小辉侠 新浪个人认证
  • 博客等级:
  • 博客积分:0
  • 博客访问:8,005
  • 关注人气:13
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

jQuery性能优化11条(二)

(2011-12-23 14:37:29)
标签:

jquery

性能优化

it

分类: jquery那些事

3、缓存jQuery对象

养成保存jQuery对象到一个变量上(就像上面的例子)的习惯。例如,不要这样做:

  1. $('#traffic_light input.on).bind('click', function(){...});  
  2. $('#traffic_light input.on).css('border', '3px dashed yellow');  
  3. $('#traffic_light input.on).css('background-color', 'orange');  
  4. $('#traffic_light input.on).fadeIn('slow');  
 

取而代之,首现保存jQuery变量到一个本地变量后,再继续你的操作。

  1. var $active_light $('#traffic_light input.on');  
  2. $active_light.bind('click'function(){...});  
  3. $active_light.css('border''3px dashed yellow');  
  4. $active_light.css('background-color''orange');  
  5. $active_light.fadeIn('slow');  
 

提示:使用$前辍表示我们的本地变量是一个jQuery包集。记住,不要在你的应该程序里出现一次以上的jQuery重复的选择操作。 额外提示:延迟存储jQuery对象结果。

如果你想在你的程序的其它地方使用jQuery结果对象(result object(s)),或者你的函数要执行多次,要把它缓存在一个全局范围的对象里。通过定义一个全局容器保存jQuery结果对象,就可以在其它的函数里引用它。

  1. // Define an object in the global scope (i.e. the window object)  
  2. window.$my ={  
  3.     // Initialize all the queries you want to use more than once  
  4.     head $('head'),  
  5.     traffic_light $('#traffic_light'),  
  6.     traffic_button $('#traffic_button')};  
  7. function do_something(){  
  8.     // Now you can reference the stored results and manipulate them  
  9.     var script document.createElement_x('script');  
  10.     $my.head.append(script);  
  11.     // When working inside functions, continue to save jQuery results  
  12.     // to your global container.  
  13.     $my.cool_results $('#some_ul li');  
  14.     $my.other_results $('#some_table td');  
  15.     // Use the global functions as you would normal jQuery result  
  16.     $my.other_results.css('border-color''red');  
  17.     $my.traffic_light.css('border-color''green');  
  18.  
 


4、更好的利用链

 
前面的例子也可以这样写:

  1. var $active_light $('#traffic_light input.on');  
  2. $active_light.bind('click'function(){...})  
  3.     .css('border''3px dashed yellow' 
  4.     .css('background-color''orange' 
  5.    .fadeIn('slow');  
 

这样可以让我们写更少的代码,使JavaScript更轻量。


5
、使用子查询

jQuery
允许我们在一个包集上附加其它的选择器。因为我们已经在本地变量里保存了父对象这样会减少以后在选择器上的性能开销。

  1. <div id="content">  
  2.     <form method="post" action="/">  
  3.         <h2>Traffic Light</h2>  
  4.         <ul id="traffic_light">  
  5.             <li><input type="radio" class="on" name="light" value="red" /> Red</li>  
  6.             <li><input type="radio" class="off" name="light" value="yellow" /> Yellow</li>  
  7.             <li><input type="radio" class="off" name="light" value="green" /> Green</li>  
  8.         </ul>  
  9.         <input class="button" id="traffic_button" type="submit" value="Go" />  
  10.     </form>  
  11. </div>  
 

例如,我们可以利用子查询缓存activeinactive lights以便后面的操作。

  1. var $traffic_light $('#traffic_light'),     
  2. $active_light $traffic_light.find('input.on'),     
  3. $inactive_lights $traffic_light.find('input.off');  
 

提示:可以用逗号隔开一次定义多个本地变量,这样可以节省一些字节。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有