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:
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:
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:
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.