This article is a mirror article of machine translation, please click here to jump to the original article.

View: 8933|Reply: 0

[JavaScript] Conversion between jQuery and DOM objects

[Copy link]
Posted on 1/4/2016 4:44:58 PM | | |

What is a jQuery object?

--- is the object generated by wrapping the DOM object through jQuery. jQuery objects are unique to jQuery and can use methods in jQuery.

Like what:

$("#test").html() means to get the html code inside the element with the ID test as test. html() is the method in jQuery

This code is equivalent to implementing code in the DOM:

document.getElementById("id").innerHTML;

Although jQuery objects are generated by wrapping DOM objects, jQuery cannot use any methods of DOM objects, nor can DOM objects use methods in jQuery. Random use will result in an error. For example: $("#test").innerHTML, document.getElementById("id").html() are all wrong.

Another thing to note is that using #id as a selector is a jQuery object and a DOM object obtained by document.getElementById("id"), which are not equivalent. See the following for the conversion between the two.

Since jQuery is different but also related, jQuery objects and DOM objects can also be converted to each other. Before converting the two, we first give a convention: if one is a jQuery object, then we add $ in front of the variable, such as: var $variab = jQuery object; If you get a DOM object, it's the same as normal: var variab = DOM object; Such an agreement is only for the convenience of explanation and distinction, and is not stipulated in actual use.



jQuery object to DOM object:

There are two conversion methods to convert a jQuery object into a DOM object: [index] and .get(index);

(1) The jQuery object is a data object, which can be obtained by using the [index] method.

For example: var $v =$("#v"); jQuery object

var v=$v[0];    DOM object

alert(v.checked) // Detects if this checkbox is checked

(2) jQuery itself provides the corresponding DOM object through the .get(index) method

For example: var $v=$("#v");  jQuery object

var v=$v.get(0);   DOM object

alert(v.checked) // Detects if this checkbox is checked




DOM object to jQuery object:

For DOM objects, you only need to wrap the DOM object with $() to get a jQuery object. $(DOM object)

For example: var v=document.getElementById("v");  DOM object

var $v=$(v);    jQuery object

After the conversion, you can use jQuery's methods as you like.

By using the above methods, you can arbitrarily convert jQuery objects and DOM objects to each other. It should be emphasized that DOM objects can only use methods in the DOM, and jQuery objects cannot use methods in the DOM.




Previous:C# implements the function of sending emails
Next:Click on the button to bring up the 60-second countdown js code
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com