Home  >  Article  >  Web Front-end  >  How jquery determines whether it is its object

How jquery determines whether it is its object

coldplay.xixi
coldplay.xixiOriginal
2020-12-21 11:40:501888browse

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");} 】.

How jquery determines whether it is its 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!

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