I've always known that jquery's initialization method is: $ (selector, context), but recently read the code only to find that the context is not only limited to find that simple:
I summed up a few points:
<p id= "N1" > <span id= "n2" > <span id= "n3" >A</span> </span> <label Id= "N4" >B</label> <span id= "N5" class= "active" > <span id= "N6" class= "Start Active" >c </span> </span> <strong id= "N7" class= "active" >D</strong> <span id= "N8" Class= "Active" >E</span> </p> <p id= "N9" class= "active" > <span id= "N10" > </span> <label id= "N11" ></label> <span id= "N12" > <span id= "N13" class= " Start >F</span> </span> </p>
1. When selector is a string CSS selector, the context is used to restrict the lookup domain. All elements that need to be searched are searched under the context.
Console.log ($ ("#n2")); N2 Console.log ($ ("#n2", $ ("#n3")));//[] Console.log ($ ("span"));//All span Console.log ($ ("span", $ (" #n2 "))); N3
2. When selector is an HTML element, the context can be a Jquery,dom object. It can also be a prop Property object.
When the context is a jquery and Dom Object , the context is used to provide the return value of the ownerdocument , The ownerdocument of the returned element is determined by the ownerdocument of the context. The default is document
In other words, if your context is under the current document, then the ownerdocument of the returned element is also DOCUMETN. If the context is under an IFRAME, then the ownerdocument of the returned element is also the document of the IFRAME.
Console.log ($ ("<span></span>") [0].ownerdocument); Generates a SPAN element, context for document Console.log ($ ("<span></span>", $ ("#n2")) [0].ownerdocument]; Generates a SPAN element, context for document Console.log ($ ("<span></span>", $ ("iframe") [0]. contentwindow.document) [0].ownerdocument); If there is an IFRAME, generate a SPAN element, the context is the document of the IFRAME
When the context is a Prop Property object , the context is used to provide the attributes of the generated element :
Console.log ($ ("<div></div>", {name: "123", width:455, Class: "Test"}) [0].outerhtml] //Chrome under: < Div name= "123" class= "Test" style= "width:455px;" ></div>
3. When selector is a jquery or dom element, thecontext is useless . This is different from what I thought before, I thought it would be filtered according to the context, but after reading the source code, and did not.
So in this case, the context is completely ignored.
Console.log ($ ($ ("#n2"))); N2 Console.log ($ ($ (" #n2"), $ ("#n3"));//N2, and no filtering Console.log ($ ($ ("#n2") [0]);//N2 Console.log ($ ($ ("#n2") [0], $ ("#n3"))); N2, and did not filter
Context in the JQuery init method