not condition fails (jQuery)
This question already has an answer here:
You need to check the number of elements found, as jQuery returns an empty collection in the event of no elements being found (which evaluates to a truthy value in the if statement):
if ( !$(".first.last").length )
I'd suggest testing against 0 explicitly, rather than relying on a falsey value, though:
if ( $(".first.last").length === 0 )
您可以通过检查从JQuery返回的第一个对象来简化它:
if (!$(".first.last")[0]){
console.log("div with both first and last classes does not exists");
} else {
console.log("it exists");
}
there are two ways:
1. check the length:
if ( !$(".first.last").length ) { // check the length if none returns 0 == false
2. check the visibility:
if ( !$(".first.last").is(':visible') ) { // returns boolean true/false
链接地址: http://www.djcxy.com/p/83688.html
上一篇: 检查jQuery中的类是否存在DIV元素
下一篇: 没有条件失败(jQuery)