[英]Why does this always return true? jQuery
I ran into something weird today. 我今天遇到了奇怪的事情。
This function always returns true for some reason, even if there is no title element in the given parent element. 即使给定的父元素中没有标题元素,该函数也总是出于某种原因返回true。
// JSEPlaceholder contains "#adiv"
if(typeof $(JSEPlaceholder).children().find("title") !== "undefined"){
alert();
}
Why is this happening? 为什么会这样呢?
This is because jQuery will always return you a jQuery object. 这是因为jQuery 总是会返回一个jQuery对象。 It may not contain any elements, but it is still an object. 它可能不包含任何元素,但仍然是一个对象。
What you want to do is check its length. 您要做的就是检查其长度。
if($(JSEPlaceholder).children().find("title").length > 0){
alert();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.