Ways to convert jquery objects to and from DOM objects

Source: Internet
Author: User

This article mainly describes the jquery object and JS in the DOM object conversion between the method, so that the jquery object can be used directly in the JS method, or JS Dom object using the method in jquery.

jquery objects and Dom objects are not the same, what is a jquery object? What is a DOM object? Let us give an example:

$ ("#aijquery")://This way gets the jquery object document.getElementById ("Aijquery")://This method gets the DOM object

jquery objects are not the same as Dom objects, and some methods of objects are different, for example, we want to get the HTML content of a tag:

document.getElementById ("Aijquery"). innerhtml;//this is the method $ ("#aijquery") of the DOM object. HTML ();//This is the method of the jquery object

Sometimes we need to convert a jquery object into a DOM object, or convert a DOM object into a jquery object, in a particular case, and convert the two objects to each other:

The DOM object is converted to a jquery object:

In general, Dom objects can be converted to jquery objects directly with $ (), such as:

$ (document.getElementById ("Aijquery"))

jquery objects are converted to DOM objects in two ways, one of which is to use jquery's built-in function get to get DOM objects, such as:

$ ("#aijquery"). Get (0);

There is one more simple way to do this, because the properties of the JQuery object are a collection, so we can take one of them out as an array:

$ ("#aijquery") [0];$ ("div") [5];//above both return DOM objects, you can use the JS method directly

The conversion between jquery objects and Dom objects is the method above, summarizing some of the ways we often use them, and the following are all correct:

$ ("#aijquery"). html (); $ ("#aijquery") [0].innerhtml; $ ("#aijquery"). EQ (0) [0].innerhtml; $ ( "#aijquery"). Get (0). innerhtml;$ ("#aijquery"). EQ (2). HTML (); $ ("#aijquery"). Get (2). InnerHTML;

Original reference: Http://www.aijquery.cn/Html/jqueryjiqiao/11.html

Ways to convert jquery objects to and from DOM objects

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.