Jquery tips (3) Changes to elements in the $ () package set

Source: Internet
Author: User

These two methods are relatively easy to confuse.
The filter method filters the current internal elements. This method accepts two parameters: a function that returns bool, or a jquery selection expression, the elements in the package set are smaller than or equal to the elements in the current package set, and the elements contained are a subset of the elements in the original package set: CopyCode The Code is as follows: <Div id = "one"> the one </div>
<Div id = "two"> <p> the two </P> </div>
<Div id = "three"> <p> the three </P> </div>
<SCRIPT type = "text/JavaScript">
Alert ($ ("Div "). filter (": Not (: First): Not (: Last)" ).html (); // out put <p> the two </P>
Alert ($ ("Div "). filter (function () {return this. id = "two"; }).html (); // output <p> the two </P> as well
</SCRIPT>

The find method searches in the current element (child element) and returns a new package set, which means the package set may increase:Copy codeThe Code is as follows: <Div id = "one"> the one </div>
<Div id = "two"> <p> the two </P> <p> </P> </div>
<Div id = "three"> <p> the three </P> </div>
<SCRIPT type = "text/JavaScript">
Alert ($ ("Div"). Find ("p"). Text (); // alert "The twothe three"
Alert ($ ("Div"). Find ("p"). Length); // alert 4 instead of original 3
</SCRIPT>

From the above, we can see that the elements in the new package set have been added.

parents () method vs closest () method
both methods are matched elements searched up by the current element. The differences are as follows: copy Code the code is as follows:




the two



for the parents method, all matching elements of the current element are added to the new packaging set and returned. The closest method only contains the elements closest to the current element, therefore, after the closest method is used, the number of elements in the current package set can be only one or zero
while the parents method does not include the elements in the current package set, the closest method contains
immediate child element vs all child elements in the current package set
the children method can return immediate child elements, however, using the find and wildcard method can return all child elements except the text node: copy Code the code is as follows:


text node here


two



we can see that the children method only contains the immediate sub-elements of the current element, and the use of find ("> * will produce the same effect "). if you want to use all the direct child elements to directly upload the "*" wildcard in "find"
back to the end () method and the andself () method
all the above methods, for example, add (), next (), nextall (), Prev (), and other methods that change elements in the package set can be returned using the end () method: copy Code the code is as follows:


text node here


two



the end () method is always offset by the latest one and the method for changing the package set, and offset other methods: copy Code the code is as follows:


text node here


two



If you need to include elements in the original package set when changing the elements in the package set, use the andself method:Copy codeThe Code is as follows: <Div id = "wrapper">
Text node here
<Div id = "two">
<P id = "p1">
The two </P>
</Div>
</Div>
<SCRIPT type = "text/JavaScript">
VaR $ A = $ ("# wrapper"). Find ("# Two"). andself ();
Alert ($ A [0]. ID); // alert two first
Alert ($ A [1]. ID); // alert wrapper after that
</SCRIPT>

We will find that alert two is selected first, because two is selected first.

PS: the liver writer code highlighting plug-in is garbled when I add Chinese characters. It's depressing to say -.-!! So the comments are all birds.

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.