Home >Web Front-end >JS Tutorial >Method to determine whether an object is a jquery object_jquery

Method to determine whether an object is a jquery object_jquery

WBOY
WBOYOriginal
2016-05-16 16:55:491223browse

This is often used when we use jquery's each to loop through, and sometimes we don't know what this refers to, because to use jquery's 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)

Judge Whether an object is a jquery object can be determined by using obj instanceof jQuery

For example:

Copy code The code is as follows:

var obj = $("div");
if(obj instanceof jQuery){
alert("This is a jQuery object");
}else{
alert("This is another object")
}

Copy code The code is as follows:

$(".otherWeek").each(function(){
console.info(this instanceof jQuery); //false
console.info($(this) instanceof jQuery ); //true
})
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