jQuery選取器最佳實務--來自jQ官網

來源:互聯網
上載者:User

標籤:style   blog   io   color   使用   sp   on   div   log   

 

1、基於ID選取器進行查詢,並且使用find方法。

1 //快速2 $("#container div.footer");3 4 //超快5 $("#container").find("div.footer");

 使用find方法會減少遍曆節點的時間。

2、使用混合選取器時,在右邊的選取器最好具體一些。

1 //最佳化前2 $("div.data .footer");3 4 //最佳化後5 $(".data div.footer");

左邊最好只使用tag或class這樣的選取器,右邊最好使用tag.class這樣的方式。

3、避免使用過多的選取器。

1 //糟糕2 $("div p.data span.test");3 4 //很好5 $("div span.test");

這樣可以減少選取器引擎遍曆節點的時間。

4、避免使用通用的選取器,會花費很多尋找時間。

1 $(".buttons > *");            // Extremely expensive.2 $(".buttons").children();   // Much better.3  4 $(".category :radio");        // Implied universal selection.5 $(".category *:radio");       // Same thing, explicit now.6 $(".category input:radio"); // Much better.

 

jQuery選取器最佳實務--來自jQ官網

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.