高效JQuery的奧秘

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   使用   sp   java   

此文章修改自http://yanhaijing.com/jquery/2013/12/05/writing-better-jquery-code/,截取了其中認為很有用的部分。

1.緩衝變數,DOM遍曆是昂貴的,所以盡量將會重用的元素緩衝。

// 糟糕h = $(‘#element‘).height();$(‘#element‘).css(‘height‘,h-20);// 建議$element = $(‘#element‘);h = $element.height();$element.css(‘height‘,h-20);

2.避免全域變數,一般來說,最好確保你的變數在函數範圍內。

// 糟糕$element = $(‘#element‘);h = $element.height();$element.css(‘height‘,h-20);// 建議var $element = $(‘#element‘);var h = $element.height();$element.css(‘height‘,h-20);

3.匈牙利命名法,在變數前加$首碼,便於識別出jQuery對象。

// 糟糕var first = $(‘#first‘);var second = $(‘#second‘);var value = $first.val();// 建議 - 在jQuery對象前加$首碼var $first = $(‘#first‘);var $second = $(‘#second‘),var value = $first.val();

4.請使用’On’,在新版jQuery中,更短的 on(“click”) 用來取代類似 click() 這樣的函數。在之前的版本中 on() 就是 bind()。自從jQuery 1.7版本後,on() 附加事件處理常式的首選方法。然而,出於一致性考慮,你可以簡單的全部使用 on()方法。

// 糟糕$first.click(function(){    $first.css(‘border‘,‘1px solid red‘);    $first.css(‘color‘,‘blue‘);});$first.hover(function(){    $first.css(‘border‘,‘1px solid red‘);})// 建議$first.on(‘click‘,function(){    $first.css(‘border‘,‘1px solid red‘);    $first.css(‘color‘,‘blue‘);})$first.on(‘hover‘,function(){    $first.css(‘border‘,‘1px solid red‘);})

5.精簡javascript,最好儘可能合并函數。

// 糟糕$first.click(function(){    $first.css(‘border‘,‘1px solid red‘);    $first.css(‘color‘,‘blue‘);});// 建議$first.on(‘click‘,function(){    $first.css({        ‘border‘:‘1px solid red‘,        ‘color‘:‘blue‘    });});

6.鏈式操作,jQuery實現方法的鏈式操作是非常容易的。

// 糟糕$second.html(value);$second.on(‘click‘,function(){    alert(‘hello everybody‘);});$second.fadeIn(‘slow‘);$second.animate({height:‘120px‘},500);// 建議$second.html(value);$second.on(‘click‘,function(){    alert(‘hello everybody‘);}).fadeIn(‘slow‘).animate({height:‘120px‘},500);

7.維持代碼的可讀性,伴隨著精簡代碼和使用鏈式的同時,可能帶來代碼的難以閱讀。添加縮緊和換行能起到很好的效果。

// 糟糕$second.html(value);$second.on(‘click‘,function(){    alert(‘hello everybody‘);}).fadeIn(‘slow‘).animate({height:‘120px‘},500);// 建議$second.html(value);$second    .on(‘click‘,function(){ alert(‘hello everybody‘);})    .fadeIn(‘slow‘)    .animate({height:‘120px‘},500);

8.選擇短路求值

// 糟糕function initVar($myVar) {    if(!$myVar) {        $myVar = $(‘#selector‘);    }}// 建議function initVar($myVar) {    $myVar = $myVar || $(‘#selector‘);}

9.繁重的操作中分離元素,如果你打算對DOM元素做大量操作(連續設定多個屬性或css樣式),建議首先分離元素然後在添加。

// 糟糕var     $container = $("#container"),    $containerLi = $("#container li"),    $element = null;$element = $containerLi.first(); //... 許多複雜的操作// bettervar     $container = $("#container"),    $containerLi = $container.find("li"),    $element = null;$element = $containerLi.first().detach(); //... 許多複雜的操作$container.append($element);

10.更好或更快的方法來使用JQuery方法。

// 糟糕$(‘#id‘).data(key,value);// 建議 (高效)$.data(‘#id‘,key,value);

11.使用子查詢緩衝的父元素,正如前面所提到的,DOM遍曆是一項昂貴的操作。典型做法是緩衝父元素並在選擇子項目時重用這些緩衝元素。

// 糟糕var     $container = $(‘#container‘),    $containerLi = $(‘#container li‘),    $containerLiSpan = $(‘#container li span‘);// 建議 (高效)var     $container = $(‘#container ‘),    $containerLi = $container.find(‘li‘),    $containerLiSpan= $containerLi.find(‘span‘);

12.避免通用選擇符,將通用選擇符放到後代選擇符中,效能非常糟糕。

// 糟糕$(‘.container > *‘); // 建議$(‘.container‘).children();

13.避免隱式通用選擇符

// 糟糕$(‘.someclass :radio‘); // 建議$(‘.someclass input:radio‘);

14.最佳化選擇符

// 糟糕$(‘div#myid‘); $(‘div#footer a.myLink‘);// 建議$(‘#myid‘);$(‘#footer .myLink‘);

15.避免多個ID選擇符

// 糟糕$(‘#outer #inner‘); // 建議$(‘#inner‘);


 

高效JQuery的奧秘

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.