通常在 JavaScript 中,我会执行如下操作来验证元素是否存在:
if (document.getElementById('lblUpdateStatus')) {
$("#lblUpdateStatus").text("");
}
但是,使用 jQuery - 我怎样才能做同样类型的事情?
最佳答案
$
返回一个匹配元素的数组,因此检查 length 属性就可以了
if ($('#lblUpdateStatus').length) {
$("#lblUpdateStatus").text("");
}
关于jquery - 如何使用 jQuery 验证 DOM 中是否存在元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1027104/