1 the method of conversion between jquery objects and Dom objects2Date: 2013-08-19Editor: www.jquerycn.cn3 When you start learning jquery, you may not be able to figure out which jquery objects are and which are DOM objects. As far as DOM objects are not explained, we have too much contact, and the following focuses on jquery and the conversion between the two. 4 When you start learning jquery, you may not be able to figure out which jquery objects are and which are DOM objects. As far as DOM objects are not explained, we have too much contact, and the following focuses on jquery and the conversion between the two. What is a jquery object? 5---is the object that is generated after wrapping the DOM object with jquery. jquery objects are unique to jquery and can be used in the jquery approach. 6 For example:7$ ("#test"). HTML () means: Gets the HTML code within the element with ID test. where HTML () is the method in jquery8 This code is equivalent to implementing the code with the DOM:9document.getElementById ("id"). InnerHTML; TenAlthough jquery objects are created after wrapping a DOM object, jquery cannot use any of the DOM object's methods, nor does the DOM object use the methods in jquery. The use of chaos will result in an error. For example: $ ("#test"). InnerHTML, document.getElementById ("id")). HTML () is wrong. OneAnother note: Using #id as the selector is the jquery object and document.getElementById ("id") The resulting DOM object, which is not equivalent. See the conversion between the two below. ASince jquery is a different but also connected, jquery objects and Dom objects can also be converted to each other. Before we convert the two, we give a contract: if one gets a jquery object, then we add $ to the variable, such as:var$variab = jquery object; If you get a DOM object, it's the same as usual:varVariab =DOM object; So the agreement is only easy to explain and distinguish, in actual use does not stipulate. - - The jquery object turns into a DOM object: the two ways to convert a jquery object to a DOM object: [index] and. get (index); -(1The jquery object is a data object that can be obtained using the [index] method to obtain the corresponding DOM object. -Such as:var$v =$ ("#v");//jquery Object - varv= $v [0];//DOM Object +Alert (v.checked)//detects if the checkbox is selected -(2jquery itself provides, through the. Get (Index) method, to get the corresponding DOM object +Such as:var$v =$ ("#v");//jquery Object A varv= $v. Get (0);//DOM Object atAlert (v.checked)//detects if the checkbox is selected - - The DOM object is turned into a jquery object: - for a DOM object, you can just wrap the DOM object with $ () and you'll get a jquery object. $ (DOM object) -Such as:varV=document.getelementbyid ("V");//DOM Object - var$v =$ (v);//jquery Object in after the conversion, you can use the JQuery method arbitrarily. - with the above methods, jquery objects and Dom objects can be converted to each other arbitrarily. It is important to note that DOM objects can use methods in the DOM, and jquery objects are not available in the DOM. to + Here are some other ways to use it: -1, Dom object to jquery object the ordinary DOM objects can generally be converted to jquery objects by $ (). * $such as: $ (document.getElementById ("MSG"))) Panax Notoginseng the jquery object is returned, and the jquery method can be used. - the2, jquery object goto DOM Object + because the jquery object itself is a collection. So if a jquery object is to be converted to a DOM object, it must take one of these items, which is generally available through the index. Asuch as: $ ("#msg") [0],$ ("div"). EQ (1) [0],$ ("div"). get () [1],$ ("TD") [5] the + These are DOM objects, and you can use the methods in the DOM, but you can't use jquery any more. - $ The following are the correct ways to do this: $$ ("#msg"). html (); -$ ("#msg") [0].innerhtml; -$ ("#msg"). EQ (0) [0].innerhtml; the$ ("#msg"). Get (0). InnerHTML;
The method of conversion between jquery objects and Dom objects