如何检查jQuery中的空对象 [英] How to check null objects in jQuery

查看:111
本文介绍了如何检查jQuery中的空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery,我想检查页面中是否存在一个元素。我写了以下代码,但是它不起作用:

  if($(#btext+ i)!= null) {
// alert($(#btext+ i).text());
$(#btext+ i).text(Branch+ i);
}

如何检查元素的存在?

解决方案

检查 jQuery常见问题 ...



您可以使用您的选择器返回的jQuery集合:

  if($('#myDiv')。length){} 


I'm using jQuery and I want to check the existence of an element in my page. I have written following code, but it's not working:

if($("#btext" + i) != null) {
    //alert($("#btext" + i).text());
    $("#btext" + i).text("Branch " + i);
}

How do I check the existence of the element?

解决方案

Check the jQuery FAQ...

You can use the length property of the jQuery collection returned by your selector:

if ( $('#myDiv').length ){}

这篇关于如何检查jQuery中的空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆