Why does this if always return true?

This question already has an answer here:

  • Is there an “exists” function for jQuery? 36 answers
  • Why does $('#id') return true if id doesn't exist? 6 answers

  • Because $(".deatils.dailyPrice:contains($)") is an object and not null objects are always evaluated as true in if tests.

    From the MDN :

    Any value that is not undefined, null, 0, NaN, or the empty string (""), and any object, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement

    You probably want

    if ($(".deatils.dailyPrice:contains($)").length) {
    

    The jQuery function constructs a jQuery object. Objects are always considered true in a truthiness evaluation such as if .

    Try this

    if( $(".deatils.dailyPrice:contains($)").length > 0 )
    
    链接地址: http://www.djcxy.com/p/14686.html

    上一篇: 如何检查一个div是否存在jquery,以及它是否无效

    下一篇: 为什么如果总是返回true?