Home >Web Front-end >JS Tutorial >How to determine whether an object is a jquery object
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");}].
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:
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!