Home > Article > Web Front-end > How jquery determines whether it is its object
Jquery's method to determine whether it is its object: To determine whether an object is a jQuery object, you can use obj instanceof jQuery, the code is [if(obj instanceof jQuery){alert("This is a jQuery object");} 】.
The operating environment of this tutorial: windows7 system, jquery3.2.1 version. This method is suitable for all brands of computers.
Recommended: jquery video tutorial
Jquery’s method of judging whether it is its object:
When we are using jquery When doing loop traversal, each often uses this
, and sometimes we don’t know what this refers to, because in order to use the jquery 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)
To determine whether an object is a jquery object, you can use obj instanceof jQuery
For example:
var obj = $("div"); if(obj instanceof jQuery){ alert("这是一个jQuery对象"); }else{ alert("这是一个其它对象") } $(".otherWeek").each(function(){ console.info(this instanceof jQuery); //false console.info($(this) instanceof jQuery); //true })
Related free learning Recommended: javascript(Video)
The above is the detailed content of How jquery determines whether it is its object. For more information, please follow other related articles on the PHP Chinese website!