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
Release: 2016-05-16 16:55:49
Original
1209 people have browsed it

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
})
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