引用
尽量在body结束前引用jQuery
如果是在</body>前引用了jQuery,那么就不需要写document.ready了,DOM已经加载完毕。
一般模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap Basic Template</title> <link href="css/bootstrap.min.css" rel="stylesheet"> <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script> <![endif]--> </head> <body> <script src="js/jquery-2.1.3.min.js"></script> <!-- Include all compiled plugins (below), or include individual files as needed --> <script src="js/bootstrap.min.js"></script> <script src="//lib.sinaapp.com/js/jquery11/1.8/jquery.min.js"></script> <script>window.jQuery || document.write('<script src="jquery1.8.min.js">\x3C/script>')</script> </body> </html>
|
选择器
1 2 3 4
| <div id="nav" class="nav"> <a class="home" >HOME</a> <a class="articles" >ARTICLES</a> </div>
|
1
| $('.home'); //1 $('#nav a.home'); //2 $('#nav').find('a.home'); //3
|
1.在整个DOM中查找class为home的a元素
2.查找id为nav的子元素,查找性能得到了很大提升
3.使用了find方法,它的速度更快
jQuery选择器的性能优先级:id>元素>class
使用ID选择器和单个类选择器都是选中元素最快的方式。
1 2 3 4 5 6
| // 糟糕 $('div#myid'); $('div#footer a.myLink'); // 建议 $('#myid'); $('#footer .myLink');
|
1 2 3 4
| // 糟糕 $('.someclass :radio'); // 建议 $('.someclass input:radio');
|
1 2 3 4
| // 糟糕 $('.container > *'); // 建议 $('.container').children();
|
1 2 3 4
| // 糟糕 if(collection.length > 0){..} // 建议 if(collection.length){..}
|
1 2 3 4 5 6 7
| //糟糕,会遍历整个DOM $(".class"); //建议,只搜索 $(".class","#id"); 性能比较: $(".class","#id") > $("#id .class") > $(".class")
|
缓存jQ对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| // 糟糕 h = $('#element').height(); $('#element').css('height',h-20); // 建议 $element = $('#element'); h = $element.height(); $element.css('height',h-20); // 糟糕 var $container = $('#container'), $containerLi = $('#container li'), $containerLiSpan = $('#container li span'); // 建议 (高效) var $container = $('#container '), $containerLi = $container.find('li'), $containerLiSpan= $containerLi.find('span');
|
1 2 3 4 5 6 7 8
| var $first = $('#first'), $second = $('#second'), value = $first.val(), k = 3, cookiestring = 'SOMECOOKIESPLEASE', i, j, myArray = {};
|
事件委托
1 2 3 4 5 6 7 8 9 10 11
| $('#t').find('td').on('click', function () { $(this).css({ 'color': 'red', 'background': 'yellow' }); }); //这样会为每个td绑上事件 $('#t').on('click', 'td', function () { $(this).css({ 'color': 'red', 'background': 'yellow' }); }); //在为100个单元格绑定click事件的测试中,两者性能相差7倍之多,选这个
|
精简代码
1 2 3 4 5 6 7 8 9 10 11 12
| // 糟糕 $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' }); });
|
1 2 3 4 5 6 7 8 9 10 11 12
| // 糟糕 $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);
|
重布局和重绘是WEB页面中最常见的也是最昂贵的两种操作。
1 2 3 4 5 6 7 8 9 10 11
| //糟糕 for(var i=0; i<10000; i++){ $("#main table").append("<tr><td>aaaa</td></tr>"); } //建议 var tablerow = ""; for(var i=0; i<10000; i++){ tablerow += "<tr><td>aaaa</td></tr>"; } $("#main table").append(tablerow);
|
链接1
链接2