Conversion of jquery objects and Dom objects to and from each other "go"

Source: Internet
Author: User

jquery objects converted to DOM objects

Only jquery objects can invoke the various functions of the JQuery class library, and the properties and methods of some DOM objects cannot be invoked on jquery, but basically the functions provided by the JQuery class library contain all the DOM operations.
The index of the jquery object holds the DOM object, so you can convert the jquery object into a DOM object by index (actually get the DOM object saved in the JQuery object).
$ ("#myphoto") [0];
When you return a DOM object by index, you can use the methods and properties of various DOM objects, such as getting the SRC attribute of a DOM object:
Alert ($ ("#myphoto") [0].SRC];

If you want to traverse each element in a jquery object, you typically use the each () function.
Echo (callback);

Callback () is a callback function, which in this function also points to the DOM element.
$ ("#myphoto"). each (function (i) {
this,src= "Test" +i+ ". jpg";
});

Dom object converted to jquery object
If you have already obtained a DOM object, you can use the jquery (elements) function to convert it to a jquery object:
Var Img=document.getelementbyid ("Myphoto");
JQuery (IMG). CSS ("border", "solid 2px #FF0000");

In the code above, IMG is the DOM object obtained using the DOM. After you convert it to a jquery object, you can change its style using the jquery object's CSS () method.

You can use "$" instead of "jquery," because within jquery there are the following implementations:
jquery=window.jquery=window.$

The "$" character can be used as a variable name in JavaScript and may appear as a prefix. However, some other class libraries or programs may have used the "$" as the variable name.

JQuery (IMG). CSS ("border", "solid 2px #FF0000");
$ (IMG). CSS ("border", "solid 2px #FF0000");

The above two statements are equivalent.
The elemients parameter of the jquery (elements) function can also be a jquery object, although it is not meaningful to say that a jquery object is converted in the second, in order to not be sure whether the type of an object is a jquery object or a DOM object. You can call this function again to convert, which guarantees that this object must be a jquery object.

Conversion of jquery objects and Dom objects to and from each other "go"

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.