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.
6. Use Stylesheets for Changing CSS on Many Elements
假如你使用 .css() 方法来改变超过20个元素的CSS,应当考虑为页面添加一个样式标签作为替代,这样做可以提升将近60%的速度。
1 // Fine for up to 20 elements, slow after that: 2 $( "a.swedberg" ).css( "color", "#0769ad" ); 3 4 // Much faster: 5 $( "<style type=\"text/css\">a.swedberg { color: #0769ad }</style>") 6 .appendTo( "head" );
7. Don’t Treat jQuery as a Black Box
把jQuery的源码当成文档,可以把它()保存在你的收藏夹内,经常的查阅参考。