Home >Web Front-end >JS Tutorial >Example code for conversion between jQuery objects and Javascript objects_jquery

Example code for conversion between jQuery objects and Javascript objects_jquery

WBOY
WBOYOriginal
2016-05-16 17:39:50873browse

Copy the code The code is as follows:

The following are some of my sharing, I love programming, I hope If you are one of the programming enthusiasts who can make more friends, please add friends and everyone will pay attention. The following articles are some things that I find useful. I will keep them as notes for myself. Of course, I hope they can help you. First of all, thank you for reading~!
If we are using jQuery as a script on the client page, it often involves the conversion of jQuery objects and Javascript objects. Because DOM objects are some object operations inherent in Javascript. DOM objects can use methods inherent in Javascript, but cannot use methods in jQuery. Therefore, we in Guangzhou Dane share a little bit of conversion codes between them for everyone to learn and refer to.

 1. Convert DOM object to jQuery object

For a DOM object that is already a DOM object, you only need to wrap the DOM object with $() to get a jQuery object, $(DOM object)

For example: var v = document.getElementById(“v”); //DOM object

 var $v = $(v); //jQuery object

After conversion, you can use jQuery methods at will.

 2. Convert jQuery object into DOM object

Two conversion methods convert a jQuery object into a DOM object: [index] and .get(index);

 (1) The jQuery object is a data object, and the corresponding DOM object can be obtained through the [index] method.

For example: var $v = $(“#v”); //jQuery object

 var v = $v[0]; //DOM object

 alert(v.checked); //Check whether this checkbox is selected

 (2) jQuery itself provides the corresponding DOM object through the .get(index) method

For example: var $v = $(“#v”); //jQuery object

 var v = $v.get(0); //DOM object ($v.get()[0] can also be used)

 alert(v.checked); //Check whether this checkbox is checked

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn