如何使用jQuery验证DOM中是否存在元素? [英] How to verify an element exists in the DOM using jQuery?

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

问题描述

通常,在JavaScript中,我会执行以下类似操作来验证元素是否确实存在:

Typically in JavaScript I do something like the below to verify an element does exist:

if (document.getElementById('lblUpdateStatus')) {
    $("#lblUpdateStatus").text("");
}

但是,使用jQuery-我该如何做相同类型的事情?

But, using jQuery - how can I do the same type of thing?

推荐答案

get方法返回匹配的DOM元素:

The get method returns the matched DOM elements:

if($("#lblUpdateStatus").get(0)){
    $("#lblUpdateStatus").click(function () { ... });
}

但是我不确定这是否是一种快速的方法.

but I am not sure if it is a fast method.

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

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