2017-09-19 6 views
0

この関数が正しく機能していない理由を理解するのにかなり苦労しています。私は.htmlvaltrim!:contains' '.textなどのバリエーションを使用して特定のクラスのスパン内の文字列をテストしようとしましたが、コンソールはオブジェクトを正しく返すように見えます。どんな助けもありがとうございます。jQueryのチェックスパンクラスと最も近いdivを非表示

if (jQuery('.price').text() == '') { 
 
    jQuery(this).closest(".box").hide(); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="box"> 
 
    test content<br> 
 
    <span class="price">$99.99</span> 
 
</div> 
 
<div class="box"> 
 
    test content<br> 
 
    <span class="price">$99.99</span> 
 
</div> 
 
<div class="box"> 
 
    test content<br> 
 
    <span class="price"></span> 
 
</div> 
 
<div class="box"> 
 
    test content<br> 
 
    <span class="price"></span> 
 
</div> 
 
<div class="box"> 
 
    test content<br> 
 
    <span class="price">$99.99</span> 
 
</div>

+0

あなたの作業コードをHTMLと共有してください –

+0

HTMLがないのでうまくいきません。 – Keith

+1

あなたはそれがうまくいかないと言いましたが、あなたが実際にやっていることは言わなかったし、問題を解決するのに十分なコードを表示しました –

答えて

3

あなたが一度にtext()すべてから.price要素を取得しているので、あなたのコードに問題があります。代わりに、それらをループする必要があり、個別にテキストを確認し、このような関連.boxを隠す:真のループの各要素に

$('.price').each(function() { 
 
    if ($(this).text().trim() == '') { 
 
    $(this).closest('.box').hide(); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="box"> 
 
    test content 
 
    <br> 
 
    <span class="price">$99.99</span> 
 
</div> 
 
<div class="box"> 
 
    test content 
 
    <br> 
 
    <span class="price">$99.99</span> 
 
</div> 
 
<div class="box"> 
 
    test content 
 
    <br> 
 
    <span class="price"></span> 
 
</div> 
 
<div class="box"> 
 
    test content 
 
    <br> 
 
    <span class="price"></span> 
 
</div> 
 
<div class="box"> 
 
    test content 
 
    <br> 
 
    <span class="price">$99.99</span> 
 
</div>

+0

ありがとうございます!!!!!!驚くべきこと:あなたの最高のあなたに感謝していただきありがとうございます! – brigitte18

-1

私はあなたの正確な要件を取得しておりません。以下のコードを試してみてください。

if (jQuery('.price').text() != '') { 
    jQuery('.price').closest(".box").hide(); 
} 
+0

の下に答えを加えました。これはすべてのリストを隠すでしょう、私は価格が表示されていないリストが必要です。これは、クラス.priceを持つすべてのスパンをチェックし、空であるかどうかを確認し、その特定スパンの最も近いディバイダを.boxクラスで非表示にして非表示にすることです。 – brigitte18

+0

https://jsfiddle.net/qajLjmkd/1/ – brigitte18

0

あなたの必要性を。

$('.price').each(function(index) { 
    if ($(this).text() == "") { 
    $(this).closest(".box").hide(); 
    } 
}); 

ただし、HTMLに問題があります。クラスの隣に2人近い人がいるので.box