Home  >  Article  >  Web Front-end  >  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-23 10:01:532341browse

Method to determine whether an object is a jQuery object: You can use the obj instanceof jQuery method 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: Windows 7 system, jquery version 3.2.1. This method is suitable for 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 to use jquery’s method, this 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).

To determine whether an object 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

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