The second parameter of the jquery function can specify the search scope for the DOM element.
The second parameter can be grouped into the following types
DOM Reference
JQuery Wrapper
Document
code example
Copy Code code as follows:
<! DOCTYPE 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 "the", the 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 a expression,
Alerts "Inputs"
Alert ("Selected" + $ (' input ', ' body '). Length + ' inputs ');
</script>
</body>
Summarize
The different types of the second argument, and the corresponding usage is shown in the following table.
type |
usage |
jQuery wrapper |
$ (' Input ', $ (' form ')). Length |
DOM Reference |
$ (' input ', document.forms[0]). length |
document |
$ (' input ', ' body '). length |