A brief introduction to jquery and Dom object _jquery

Source: Internet
Author: User

In the first study of jquery, it is often impossible to distinguish between DOM objects and jquery objects, so let's take a brief look at the relationships and differences between them.

1.DOM Object (Document object Model)

Document Object model, each DOM can be represented as a tree, for example, the following is a simple page code:

Represents the DOM as:

We can get the nodes in the tree through the Getelementsbytayname or Getelementsbytayid in JS, such as the DOM object, the DOM can use the JS method, for example:

Copy Code code as follows:
var domobj=document.getelementsbytayname ("Name"); Get DOM Object
var objhtml=domobj.innerhtml; Using the innerHTML method in JS 2.2

2.Jquery objects

The jquery object is the object that is created after the DOM object is wrapped through jquery, which is unique to jquery and can invoke methods in jquery, for example:

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

jquery objects cannot invoke any method of a DOM object, for example:

Copy Code code as follows:
$ ("#foo"). InnerHTML//There will be an error

Conversion between 3.DOM objects and jquery objects

Before we convert them, we must first define the style of the variable, such as when defining a jquery object, add a $ symbol, for example:

 var $obj =jquery Object

When you define a DOM object, you do not need to add any symbols to help us distinguish what the variable is, and to improve the readability of the code, for example:

Var Domobj=dom object

When the jquery class library does not have the method we want or our approach to jquery is not clear, we can transform it into a DOM object, and there are 2 ways to convert the jquery object into a DOM object----[Index]/get (Index),
(1) The JQuery object is an array object, and a DOM object is obtained by [index], as follows:

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

(2) Another method is provided by jquery itself, obtained by getting (index) to a DOM object, for example:

var $obj =$ ("#sc");
var obj= $obj. Get (0);
Alter (obj.checked);

4.DOM object converted to jquery object

A DOM object can be converted to a jquery object by only $ (), for example:

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

The above mentioned is the entire content of this article, I hope you can enjoy.

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.