The second argument to the jQuery function gets the DOM element in the specified context

  • 2020-03-30 03:02:36
  • OfStack

The second argument to the jQuery function specifies the search scope for DOM elements.

The second parameter can be divided into the following types

DOM reference
JQuery wrapper
The document

Code sample
 
<!DOCTYPE html> 
<html> 
<body> 
<form> 
<input name="" type="checkbox"/> 
<input name="" type="radio"/> 
<input name="" type="text"/> 
<input name="" type="button"/> 
</form> 
<form> 
<input name="" type="checkbox"/> 
<input name="" type="radio"/> 
<input name="" type="text"/> 
<input name="" type="button"/> 
</form> 
<input name="" type="checkbox"/> 
<input name="" type="radio"/> 
<input name="" type="text"/> 
<input name="" type="button"/> 

<script type="text/javascript" src="jquery-1.11.1.js"></script> 
<script type="text/javascript"> 
// search within all form elements, using a wrapper for context, 
// alerts "8 inputs" 
alert("selected" + $('input', $('form')).length + ' inputs'); 

// search with the first form element, using DOM reference as the context, 
// alerts "4 inputs" 
alert("selected" + $('input', document.forms[0]).length + ' inputs'); 

// search within the body element for all input elements using an expression, 
// alerts "12 inputs" 
alert("selected" + $('input', 'body').length + ' inputs'); 
</script> 
</body> 
</html> 

conclusion

The different types of the second parameter correspond to the following table. type usage JQuery wrapper The < code > $(' input '$(' form')). The length < / code > DOM reference < code > $(' input, document forms [0]). The length < / code > The document The < code > $(' input ', 'body'.) length < / code >

Related articles: