1

jquery を使用して div 要素内の文字列をチェックしていますが、dom に then $ が含まれていない場合でも、常に if never を返します。

$('.bw-switcher').on('click', function() {
    $(this).toggleClass('toggledrate', 1000);
    $('.toggledrate > .bwswitch-button:before').toggle('fast');
    $('.toggle_rates').toggle('fast');
    if ($(".deatils.dailyPrice:contains($)")) {
        $('.deatils.dailyPrice').toggle('fast');
        console.log('I am the if');
    } else {
        console.log('I am the else');
        $('.deatils.dailyPrice').toggle('fast').prepend('$');
    }
    $('.toggledrate > .bwswitch-button:after').toggle('fast');
});
4

2 に答える 2

9

$(".deatils.dailyPrice:contains($)")はオブジェクトであり、null ではないオブジェクトは常にテストのようtrueに評価されるためです。if

MDNから:

未定義、null、0、NaN、または空の文字列 ("") 以外の値、および値が false である Boolean オブジェクトを含むすべてのオブジェクトは、条件ステートメントに渡されたときに true と評価されます。

あなたはおそらくしたいです

if ($(".deatils.dailyPrice:contains($)").length) {
于 2013-08-13T19:45:03.183 に答える