Mutual transformation of jquery object and DOM object to implement code _jquery

Source: Internet
Author: User
jquery objects and Dom objects convert to each other
jquery Objects and Dom objects

The jquery object is the object that is generated after the DOM object is wrapped through jquery. jquery objects are unique to jquery, which can use the methods in jquery, but cannot use DOM methods, such as $ ("#img"). attr ("src", "test.jpg"); The $ ("#img") here is the jquery object;

Dom objects are some of the object operations inherent to JavaScript. DOM objects can use the intrinsic methods of JavaScript, 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, is correct, but $ ("#img"). src= "Test.jpg" or document.getElementById ( "img"). attr ("src", "test.jpg"); are wrong.
To say an example: this is what I often write in jquery: this.attr ("src", "test.jpg"); But it was a mistake. This is actually a DOM object, and. attr ("src", "test.jpg") is a jquery method, so it goes wrong. To solve this problem, convert the DOM object into a jquery object, such as $ (this). attr ("src", "test.jpg");

1.DOM objects into jquery objects:
For already a DOM object, you can get a jquery object by wrapping the DOM object with $ (). $ (DOM object)
such as: Var V=document.getelementbyid ("V"); Dom Object
var $v =$ (v); jquery objects
After the conversion, you can use the JQuery method at will.

The 2.jQuery object is turned into a DOM object:
Two conversions convert a jquery object into a DOM object: [index] and. get (index);
(1) The jquery object is a data object, which can be obtained through the method of [index] to get the corresponding DOM object.
such as: Var $v =$ ("#v"); jquery objects
var v= $v [0]; Dom Object
Alert (v.checked)//Detect if this checkbox is selected
(2) jquery itself provides, through the. Get (Index) method, the corresponding DOM object
such as: Var $v =$ ("#v"); jquery objects
var v= $v. Get (0); Dom Object
Alert (v.checked)//Detect if this checkbox is selected

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.