Conversion between jquery objects and DOM objects
Jquery object and DOM object
Jquery objects are the objects generated after DOM objects are encapsulated by jquery. Jquery objects are unique to jquery. They can use methods in jquery, but do not use Dom methods. For example, $ ("# IMG "). ATTR ("src", "test.jpg"); $ ("# IMG") Here is the jquery object;
DOM objects are some object operations inherent in JavaScript. DOM objects can use JavaScript's inherent methods, but they cannot use the methods in jquery. For example, document. getelementbyid ("IMG"). src = "test.jpg"; here document. getelementbyid ("IMG") is the DOM object;
$ ("# IMG "). ATTR ("src", "test.jpg"); and document. getelementbyid ("IMG "). src = "test.jpg"; is equivalent and correct, but $ ("# IMG "). src = "test.jpg"; or document. getelementbyid ("IMG "). ATTR ("src", "test.jpg"); all errors.
Here is an example: This is what I often write when writing jquery: This. ATTR ("src", "test.jpg"); but it is an error. In fact, this is a DOM object, and. ATTR ("src", "test.jpg") is a jquery method, so an error occurs. To solve this problem, we need to convert the DOM object to a jquery object, such as $ (this). ATTR ("src", "test.jpg ");
1. convert a DOM object to a jquery object:
For a DOM object, you only need to wrap the DOM object with $ () to obtain a jquery object. $ (DOM object)
For example, VAR v = Document. getelementbyid ("v ");// DOM object
VaR $ v = $ (v ); // Jquery object
After conversion, you can use the jquery method at will.
2. convert a jquery object to a DOM object:
Two conversion methods are used to convert a jquery object to a DOM object: [Index] And. Get (INDEX );
(1) The jquery object is a data object. You can use the [Index] method to obtain the corresponding DOM object.
For example, VAR $ v = $ ("# V"); // jquery object
VaR v = $ V [0]; // DOM object
Alert (V. Checked) // Check whether the checkbox is selected
(2) jquery provides the. Get (INDEX) method to obtain the corresponding DOM object.
For example, VAR $ v = $ ("# V ");// Jquery object
VaR v = $ v. Get (0 ); // DOM object
Alert (V. Checked)// Check whether the checkbox is selected