Conversion between jQuery objects and dom objects.

Source: Internet
Author: User

Conversion between jQuery objects and dom objects.

I have never understood some methods for directly using JavaScript For objects obtained through jQuery. Now I know about this case, the objects obtained by jQuery are not the same as those obtained by getElementById. Therefore, some new users are confused about why $ {"# Element"} cannot be directly innerHTML. This is the reason. For the solution, see the following.

Conversion between jQuery objects and dom objects
Only jquery objects can use methods defined by jquery. Pay attention to the differences between dom objects and jquery objects. When calling methods, pay attention to dom objects or jquery objects.
Common dom objects can be converted to jquery objects through $. A front-end UI framework that can improve development efficiency by 500%!

For example:

$(document.getElementById("msg"))
 

This is a jquery object. You can use the jquery method.
Because the jquery object itself is a set. Therefore, if a jquery object is to be converted to a dom object, You must retrieve one of them. Generally, you can retrieve it through indexes.
For example:

$("#msg")[0],$("div").eq(1)[0],$("div").get()[1],$("td")[5]
 

These are dom objects. You can use methods in dom, but you cannot use Jquery methods. A front-end UI framework that can improve development efficiency by 500%!
The following statements are correct:

$("#msg").html();$("#msg")[0].innerHTML;$("#msg").eq(0)[0].innerHTML;$("#msg").get(0).innerHTML;
 


Convert a DOM object to a jQuery object:

This means that v is a DOM object, while $ v is a jquery object. If you want to call it later, $ v is used. Of course, $ is not required. You can also use other variable names, such as var jq_obj = $ (v)

Convert a jquery object to a dom object

JQuery returns an array no matter whether one or more elements are obtained.
The elements to be obtained are placed in an array object. (We can also think that the DOM object is stored in the array)
Since the array can write 0 or 1 or n, it depends on how many DOM objects are obtained.
Therefore, when cm [0] is used to obtain the first DOM object obtained by jquery
Cm [1] takes the second and so on.

What you wrote above is # That is, the ID selector must obtain an element, or the element does not exist or is 0.
Therefore, var cm = $ ("clickMe"); if an element is obtained, cm [0] cannot return an error ..
Cm. length check is supported.

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.