Home > Web Front-end > JS Tutorial > JQuery Tips(2) What you don't know about $() wrapper_jquery

JQuery Tips(2) What you don't know about $() wrapper_jquery

WBOY
Release: 2016-05-16 18:39:15
Original
1041 people have browsed it

I think this is easy to understand. JQuery objects wrapped by $() always appear in the form of a collection. Even if there is only one object in the packaging set.

Copy code The code is as follows:





The contents of the two DIVs selected above will be changed to "hi"
The order of the elements in the packaging set
Among the elements wrapped by JQuery, the internal order contained in the packaging set is according to HTML The streams are arranged from first to last instead of the selection order:
Copy the code The code is as follows:

here is a

here is b



As you can see from the above code, although b is selected first, when the alert is executed, "here is a" will pop up first and then " here is b”
Conversion of JQuery object and DOM
First of all, DOM is converted into JQuery object. This is very easy, just include it in $(). But one thing to note is that when JQuery Within the event of the wrapped element, this always points to the current object:
Copy code The code is as follows:

here is a

here is b



Converting elements in the JQuery packaging set to DOM is also very simple for JQuery. In most cases, JQuery’s get method is used
Copy code The code is as follows:

here is a

here is b



As can be seen from the above, adding the index as a parameter through the get method will return the DOM object of the index value , without adding parameters, it will return the entire array in the JQuery packaging set
Another convenient method is to add the array symbol directly after the JQuery packaging set. You can think of the above Jq[0] as the convenience of Jq.get(0) Method:-)
Check the number of elements in the current JQuery packaging set
In many cases, we need to check the number of elements in the JQuery packaging set. We can directly use the length attribute of the packaging set (this attribute is in VS No prompt)
Copy code The code is as follows:

div id="a"> here is a

here is b



This attribute can also be used directly Check whether the current packaging set is empty
Copy code The code is as follows:

here is a

here is b



The above two alerts will be executed. The second method determines whether the wrapping set is empty by detecting whether the first element in the current wrapping set is empty.
The wrapping set is also "not always" under certain circumstances. Set-oriented”
Didn’t you say you were always set-oriented? Why has it changed again? In fact, it is indeed collection-oriented, but this is not the case when using certain methods of JQuery to extract, such as the following code:
Copy code The code is as follows:

here is a

here is b


The above code will only alert the id of the first div. So what should we do in this case? Yes, use JQuery's Each method. The each method will traverse each element in the package set:
Copy the code The code is as follows:

here is a

here is b



The above code will execute two alerts:-)
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template