jquery源码:
1 jQuery = function( selector, context ) { 2 3 // The jQuery object is actually just the init constructor 'enhanced' 4 // Need init if jQuery is called (just allow error to be thrown if not included) 5 return new jQuery.fn.init( selector, context ); 6 },
对于这个context的解释是这样的:
他可以控制前面selector的范围,context既可以是jquery对象,也可以是DOM对象,不过区别是,jquery对象的context依然为document,而DOM对象的context为你选择的DOM对象。
如图:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>conText</title> </head> <body> <div> <ul class="cc"> <li class="aa"></li> <li class="bb"></li> <li class="aa"></li> <li class="aa"></li> <li class="aa"></li> </ul> <ul class="dd"> <li class="aa"></li> <li class="bb"></li> <li class="aa"></li> <li class="ff"></li> <li class="aa"></li> </ul> </div> <script src="http://libs.baidu.com/jquery/1.8.3/jquery.min.js"></script> <script type="text/javascript"> $('.bb',$('.dd')).css('background','red') var dd = document.getElementsByClassName('dd'); console.log($('.bb',$('.dd'))); console.log($('.bb',dd[0])); </script> </body> </html>