$(function() {
if ( !$(".first.last") ) {
console.log("div with both first and last classes does not exists")
} else {
console.log("it exists");
}
});
我想检查一个 div 是否同时具有 first
和last
类不存在,但检查失败。
最佳答案
您需要检查找到的元素数量,因为如果没有找到元素,jQuery 将返回一个空集合(在 if 语句中计算结果为真值):
if ( !$(".first.last").length )
不过,我建议显式地针对 0 进行测试,而不是依赖于错误值:
if ( $(".first.last").length === 0 )
关于jquery - if-not 条件失败 (jQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23619973/