jQuery - 如何检查元素是否存在? [英] jQuery - how to check if an element exists?

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

问题描述

我知道您可以测试 width()height() 但是如果元素的 display 属性设置为 none 呢?还有什么其他值需要检查以确保元素存在?

I know that you can test for width() or height() but what if the element's display property is set to none? What other value is there to check to make sure the element exists?

推荐答案

您可以使用 length 来查看您的选择器是否匹配任何内容.

You can use length to see if your selector matched anything.

if ($('#MyId').length) {
    // do your stuff
}

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

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