The difference between jquery object and Dom object and its transformation

Source: Internet
Author: User

jquery objects are objects that are produced after wrapping a DOM object through jquery.

DOM objects can use the methods in JS

jquery objects cannot use methods in DOM objects;

As an example:

$ ("#foo"). html ();//This is the way of jquery.

Equivalent to the following

document.getElementById ("foo"). innerhtml;//this is the JS method;

1.jQuery objects converted to DOM objects

(1) The JQuery object is an array-like object that can be passed "index" and get (index).

var $cr =$ ("#fd");//jquery Object

var cr= $cr [0];//dom Object

(2) Get (index) method to get the corresponding DOM object

var $cr =$ ("#fd");//jquery Object

var cr= $cr. Get (0);//dom Object

2.DOM objects converted to jquery objects

var Cr=document.getelementbyid (' Cr ');//dom object

var $cr =$ (CR);//jquery Object

Attention:

$ (' #cr ') gets the object forever, even if the element is not on the page. Therefore, when you want to use jquery to check whether an element exists on a Web page, you cannot use the following code directly:

if ($ (' #cr ')) {

Do

}

Instead, it should be judged based on the length of the obtained element, the code is as follows:

if ($ (' #cr '). length>0) {//do}

or converted to a DOM object to judge, the code is as follows:

if ($ ("#cr") [0]) {//do}

The difference between jquery object and Dom object and its transformation

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.