jquery objects and JavaScript objects that are DOM objects convert to each other _jquery

Source: Internet
Author: User

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 the 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.

Another example is this, which is often written in jquery: This.attr ("src", "test.jpg"); but it's an error, actually this is a DOM object, and. attr ("src", "test.jpg") is a jquery method , so it went wrong. To solve this problem, convert the DOM object into a JQuery object, such as $ (this). attr ("src", "test.jpg");

1. DOM objects Turn into JQuery objects

For a DOM object, you can get a JQuery object by wrapping the DOM object with $ (), $ (DOM object) Note: VAR is defined as a variable

Such as:

var v = document.getElementById ("V"); Dom Object
var $v = $ (v);//jquery Object

After conversion, you can use the JQuery method as you like.

2. JQuery objects Turn into DOM objects

Two conversions speak of a JQuery object converted to 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 Object
var v = $v [0];//dom object
alert (v.checked);//Detect if this checkbox is selected

(2) JQuery itself provides the corresponding DOM object through the. Get (Index) method

Such as:

var $v = $ ("#v"); JQuery Object
var v = $v. Get (0);//dom object ($v. got () [0] can also)
alert (v.checked);//Detect if the checkbox is selected

By using these methods, you can arbitrarily transform the jquery objects and Dom objects, and it is necessary to emphasize that DOM objects can use methods in the DOM, and JQuery objects are methods that cannot be used in the DOM.

Related Article

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.