Home >Web Front-end >JS Tutorial >How to determine whether an object is a jquery object

How to determine whether an object is a jquery object

coldplay.xixi
coldplay.xixiOriginal
2020-11-19 11:19:182725browse

Method to determine whether an object is a jQuery object: Use [obj instanceof jQuery] to determine, the code is [if (obj instanceof jQuery){alert("This is a jQuery object");}].

How to determine whether an object is a jquery object

The operating environment of this tutorial: windows10 system, jquery2.2.4, this article is applicable to all brands of computers.

Method to determine whether an object is a jquery object:

When we use jquery's each to do loop traversal, we often use this, and sometimes we I don't know what this refers to, because in order to use jquery's method, the object must be a jquery object.

In addition, to determine the type of a JavaScript object, you can use typeof, but typeof can only determine the basic object of js (string, boolean, number, object).

Determine an object Whether it is a jquery object, you can use obj instanceof jQuery

var obj = $("div");
if (obj instanceof jQuery) {
    alert("这是一个jQuery对象");
} else {
    alert("这是一个其它对象")
}
 
obj.each(function() {
    console.log(this instanceof jQuery); //false
    console.log($(this) instanceof jQuery); //true
})

Others:

  • ##$(this)[0]

  • $(this).get(0)

  • ##$(this).toArray()[ 0];

  • The above three writing methods are equivalent

It is not conversion, it is to obtain the internal DOM object reference.

jQuery has There are 2 cores, one is a query and the other is a method plug-in

$('..')

is a factory method that calls the query internally (according to the css positioning method to obtain all dom references that meet the conditions , there are other special circumstances) And return a jQuery object. This object is an extended Array. All queried DOM are placed in this Array.

then

get(index )

The internal code is return this[index]

Related free learning recommendations:
JavaScript

(video)

The above is the detailed content of How to determine whether an object is a jquery object. For more information, please follow other related articles on the PHP Chinese website!

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