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

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

问题描述

我正在使用 jQuery,我想检查我的页面中某个元素是否存在.我已经编写了以下代码,但它不起作用:

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?

推荐答案

检查 jQuery 常见问题...

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

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

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

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

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