The second parameter of the jquery function gets the DOM element in the specified context _jquery

Source: Internet
Author: User
Tags wrapper
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.