"jquery" jquery objects and JavaScript objects that are DOM objects are converted to each other

Source: Internet
Author: User

jquery objects are objects that are produced after wrapping a DOM object through jquery. jquery objects are unique to jquery and can be used in jquery, but cannot use the DOM method, for example: $ ("#img"). attr ("src", "test.jpg"); The $ ("#img") here is the JQuery object.

Dom objects are some of the inherent object operations of JavaScript. DOM objects can use JavaScript's inherent methods, but 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 all wrong.

Another example is this, which is often written when writing jQuery: this.attr ("src", "test.jpg"), but it is a DOM object, and. attr ("src", "test.jpg") is the jquery method , so something went wrong. To solve this problem, convert the DOM object to a JQuery object, such as $ (this). attr ("src", "test.jpg");

1. DOM object into JQuery object

For a DOM object, you just need to wrap the DOM object with $ () and you can get a JQuery object, $ (DOM object) Note: VAR is a variable defined

Such as:

var // DOM Object var // JQuery Object
After conversion, you can use JQuery in any way.

2. JQuery objects into DOM objects

Two ways to convert a JQuery object into a DOM object: [index] and. get (index);

(1) JQuery object is a data object, you can get the corresponding DOM object by means of [index].

Such as:
var // JQuery Object var // DOM Object // detects if the checkbox is selected

(2) JQuery itself provides, through the. Get (index) method to get the corresponding DOM object

Such as:
var // JQuery Object var // Dom Object ($v. get () [0] can also) // detects if the checkbox is selected
With these methods, jquery objects and Dom objects can be converted arbitrarily to each other, and it is emphasized that DOM objects can use methods in the DOM, and jquery objects are not available in the DOM. SOURCE Link: jquery objects and JavaScript objects that are DOM objects are converted to each other

"jquery" jquery objects and JavaScript objects that are DOM objects are converted to each other

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.