Describes Jquery and DOM objects, and describes JqueryDOM objects.

Source: Internet
Author: User

Describes Jquery and DOM objects, and describes JqueryDOM objects.

In the first learning of jquery, we often fail to distinguish between DOM objects and Jquery objects. Next we will briefly discuss the relationships and differences between them.

1. DOM Object (Document Object Model)

The object model of the document. Each DOM can be represented as a tree. For example, the following is a simple webpage code:

DOM:

We can use getelementsbyuncname or getelementsbyuncid in JS to obtain the nodes in the tree. The elements obtained in this way are DOM objects. DOM can use methods in JS, such:
Copy codeThe Code is as follows: var domobj = document. getelementsbyuncname ("Name"); // get the DOM object
Var objhtml = domobj. innerHTML; // use the innerHTML method 2.2 in JS.

2. Jquery object

Jquery objects are generated after DOM objects are packaged by Jquery. They are unique to Jquery and can call methods in jquery, such:

$("#foo").HTML();

Jquery objects cannot call any methods of DOM objects, for example:
Copy codeThe Code is as follows: $ ("# foo"). innerHTML // an error will occur.

3. Mutual conversion between DOM objects and Jquery objects

Before converting them, we must first define the style of variables, such as adding a $ symbol when defining a Jquery object. For example:

Var $ obj = Jquery object

When defining a DOM object, you do not need to add any symbols. This helps us to differentiate what objects are variables and improve code readability. For example:

Var domobj = DOM object

When the Jquery class library does not have the method we want or we are not clear about Jquery's method, we can convert it to a DOM object, there are two ways to convert a Jquery object to a DOM object ---- [index]/get (index ),
(1) The jquery object is an array object and a DOM object is obtained through [index]. The Code is as follows:

var $obj=$("#sc");var obj=$obj[0];alter(obj.checked);

(2) Another method is provided by Jquery itself. get the DOM object through get (index), for example:

var $obj=$("#sc");var obj=$obj.get(0);alter(obj.checked);

4. convert a DOM object to a Jquery object

The DOM object can be converted to a Jquery object through $ (), for example:

var obj=document.getelementsByTayName("Name");var $obj=$(obj);

The above is all the content of this article. I hope you will like it.

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.