JQuery objects and Dom object conversions

Source: Internet
Author: User

First, we need to know why we need to transform both, because the methods provided by the two cannot be shared.

Like what:

$ ("#id"). InnerHTML; document.getElementById (ID). HTML ();

The above code is all wrong,

The first line, $ ("#id") is the jquery object, and innerHTML is the method of the DOM object;

The second line, document.getElementById (ID) is the DOM object, and HTML () is the method of the JQuery object;

As we begin to say, the methods provided by both cannot be used for non-objects, that is, the jquery object cannot use the methods provided by the DOM object, nor can the DOM object use the methods provided by the JQuery object.

But sometimes we need to use methods that are not available in this object, and at this point we need to convert both.

Style conventions: We agreed that the jquery object variable name is preceded by the $ symbol.

Let $str = $ (". txt")//JQuery object Let str = $str. Get (0);   Dom Object

  

1.jQuery objects converted to Dom objects

(1) The JQuery object is an array object, so we can use [index] to convert it to a DOM object:

  Let $str = $ (". txt")//JQuery object let  str = $ (". txt") [0];//Dom Object

(2) The JQuery object itself also provides a way to convert to a DOM object, get (Index):

  Let $str = $ (". txt")//JQuery object let  str = $str. Get (0);   Dom Object

 

The 2.Dom object is converted to a jquery object:

The DOM object is converted to a JQuery object, and a jquery object can be obtained by wrapping the DOM object with $ ().

Let str = document.getElementById (ID); Dom Object Let $str = $ (str); JQuery Object

  

We can print out both by Console.log ()

In this way, we can visually detect whether the current object is a DOM object or a jquery object.

Finish

JQuery objects and Dom object conversions

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.