您的位置:首页 > 技术中心 > 前端框架 >

jquery如何判断是否是其对象

时间:2020-12-21 12:42

jquery判断是否是其对象的方法:判断一个对象是否为jquery对象可以用obj instanceof jQuery,代码为【if(obj instanceof jQuery){alert("这是一个jQuery对象");}】。

本教程操作环境:windows7系统、jquery3.2.1版本,该方法适用于所有品牌电脑。

推荐:jquery视频教程

jquery判断是否是其对象的方法:

当我们在用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
})

相关免费学习推荐:javascript(视频)

以上就是jquery如何判断是否是其对象的详细内容,更多请关注www.gxlsystem.com其它相关文章!

本类排行

今日推荐

热门手游