×

判断一个对象是否为jquery对象的方法

作者:Terry2017.02.14来源:Web前端之家浏览:9111评论:0
关键词:JQueryJS
当我们在用jquery的each做循环遍历的时候常常会使用到this,而有时候我们不知道this所指的到底是什么,因为要使用jquery的方法 前提此对象必须是jquery对象。

另外要判断一个javascript的对象是什么类型,可以使用typeof,
但是typeof只能判断出js的基础对象(string,boolean,number,object)

判断一个对象是否为jquery对象可以用 obj instanceof jQuery

例如:
复制代码 代码如下:

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

您的支持是我们创作的动力!
温馨提示:本文作者系Terry ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://jiangweishan.com/article/svg1487001600620.html

网友评论文明上网理性发言已有0人参与

发表评论: