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?

青灯夜游
Release: 2020-11-30 14:16:46
Original
3627 people have browsed it

Method to determine whether an object is a jquery object: use the instanceof operator, the syntax "if(object instanceof jQuery){//jQuery object}else{//other objects}", object is used to specify the need to judge Object.

How to determine whether an object is a jquery object?

Related recommendations: "jQuery Video"

The operating environment of this tutorial: windows7 system, jquery3.5 version, This method works for all brands of computers.

Method to determine whether an object is a jquery object:

When writing a recursive function to input a JSON object, the JSON data includes jQuery objects, this is It resulted in too many loop errors. I searched online for a long time and found that there was none. I thought I could use instanceof Date to determine whether it is a date object. Why not give it a try? The results show that obj instanceof jQuery can determine whether an object is a jQuery object.

By the way, 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, ect.). I don’t understand. Just google it. As for object objects, many objects in js are objects. For example, Date object is a kind of object. Fortunately, the sky is the limit, there is an instanceof that can determine whether it is a date or jquery type.

Let’s be more detailed. Date is actually an object, and so is jQuery. Then after you create a new Date, the object becomes an instance, so you have to use instanceof to determine the source of the instance.

var obj = $("body");
if(obj instanceof jQuery){
  alert("这是一个jQuery对象");
}else{
  alert("这是一个其它对象")
}
Copy after login

instanceof operator is used to detect whether the prototype attribute of the constructor appears on the prototype chain of an instance object

Note: When jquery objects are named, they usually start with $, such as var $ps = $ ('p'), the advantage of this is that you can know at a glance that $ps is a jquery object, and the readability of the program is greatly improved.

For more programming related knowledge, please visit: Programming 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!

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