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.