Home > Web Front-end > JS Tutorial > Analysis of the difference between Jquery objects and Dom objects_jquery

Analysis of the difference between Jquery objects and Dom objects_jquery

WBOY
Release: 2016-05-16 16:30:49
Original
1732 people have browsed it

Before discussing, let’s agree on the style of defining variables.

If the obtained object is a jQuery object, add $ before the variable, for example:

Copy code The code is as follows:

var $variable = jQuery object;

If the DOM object is obtained, it is defined as follows:

Copy code The code is as follows:

var variable = DOM object;


jQuery objects cannot use methods in DOM, but if you are not familiar with the methods provided by jQuery objects, or jQuery does not encapsulate the desired methods, and you have to use DOM objects, there are two methods below. jQuery provides two methods to convert a jQuery object into a DOm object, namely [index] and get(index).

1. The jQuery object is an array object, and the corresponding DOM object can be obtained through the [index] method.

Copy code The code is as follows:

var $cr = $("#cr"); //jQuery object
var cr = $cr[0] //DOM object
alert(cr.checked) //Check whether this checkbox is checked

2. Obtain the corresponding DOM object through the get(index) method.

Copy code The code is as follows:

var $cr = $("#cr");
var cr = $cr.get(0);
alert(cr.checked);

For a DOM object, you only need to wrap the DOM object with $() to get a jQuery object. The method is $(DOM object).

Copy code The code is as follows:

var cr = document.getElementByID("cr"); //DOM object
var $cr = $(cr);

Summary:

1. The get method in jQuery actually obtains Dom elements ($(this).get(0) and $(this)[0])

2. The eq, first, last and other methods in the jQuery method are all returned Jquery objects

3. Only DOM objects can use methods in DOM. jQuery objects cannot use methods in DOM. jQuery objects provide a more complete set of tools for manipulating the DOM.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template