如何使用jQuery验证DOM中是否存在元素?

如何使用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.