2016-11-29 5 views
1

がある場合には、以下の要素の文字列が0私はクラスを設定する必要がコロン接頭語

よりも高い値を持っている場合は数を比較すると、スクリプトは私が強い部分とコロンを削除した場合に動作しますが、Iこれを制御できないので、どうすればこの目標を達成できますか?

スクリプト:

$('.stockinfo.col4').filter(function(index){ 
return parseInt(this.innerHTML) > 0; 
}).addClass("instocktest"); 

HTML:

<div class="stockinfo col4"><strong>Stock</strong>: 0<span></span></div> 
<div class="stockinfo col4"><strong>Stock</strong>: 50<span></span></div> 

答えて

1

任意のHTMLタグなしのテキストを取得するには.textContentを使用してください。そして、ここ数

$('.stockinfo.col4').filter(function(index) { 
 
    return parseInt(this.textContent.replace(/\D/g, '')) > 0; 
 
}).addClass("instocktest");
.instocktest { 
 
    background-color: green; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="stockinfo col4"><strong>Stock</strong>: 0<span></span> 
 
</div> 
 
<div class="stockinfo col4"><strong>Stock</strong>: 50<span></span> 
 
</div>

+0

動作しません - 要素に何も起こりません。 – user3344734

+0

DIVにコロンの前にもっと多くのテキストが含まれているのを見ていませんでした。私はregexpを使うように変更しました。 – Barmar

+0

今は完璧に動作します - ありがとう! – user3344734

0

するtext()は、よりがある場合と一致するようにあなたのdiv

$('.stockinfo.col4').filter(function(index){ 
return parseInt($(this).text()) > 0; 
}).addClass("instocktest"); 
+0

作業?あなたは何をした?これについての本当のテストはどこですか? – Justinas

+0

@Justinasここで見られるような非常に変わった変化は何ですか?追加されるtext()メソッドの使用です。 – ScanQR

+0

'text()'はjQueryメソッドで、 'this'はDOM要素ですから、' this.text() 'は' $(this).text() 'でなければなりません。 – Barmar

0

使用正規表現でコンテンツを生成するかどうかをチェックすることができ、これを試してみてください内容の1つ以上:

$(document).ready(function() { 
 
    $('.stockinfo.col4').filter(function(index) { 
 
    return $(this).text().match(/0/g).length >= 2; 
 
    }).addClass("instocktest"); 
 
})
.instocktest { 
 
    color: red; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="stockinfo col4"><strong>Stock</strong>: 0<span></span> 
 
</div> 
 
<div class="stockinfo col4"><strong>Stock</strong>: 5000<span></span> 
 
</div> 
 
<div class="stockinfo col4"><strong>Stock</strong>: 50 and 00<span></span> 
 
</div> 
 
<div class="stockinfo col4"><strong>Stock</strong>: 50 and 0<span></span> 
 
</div>

+0

私はOPが "1つ以上のゼロ"ではなく "ゼロ以上"と言っていると思います。 – connexo

+0

ええ、多分私は不明であった - コネキシオは正しい、これは私のために働くdoesnt。 – user3344734

+0

OPはdivにコンテンツがあるかどうかを確認したいだけですか? – ScanQR

1

に変換する前に、数字以外のすべてを削除し、別のソリューションです。これをしようとするのはなぜ

$('.stockinfo.col4').filter(function(index){ 
    var number=$(this).find('strong')[0].nextSibling.nodeValue.replace(/[^0-9]/g, ''); 
    return parseInt(number) > 0; 
}).addClass("instocktest"); 

fiddle

+0

これは不運にも役に立たない – user3344734

+0

@ user3344734、私にとっては完璧に働いています。私はフィドルで答えを更新しました。 jqueryプラグインを含めるのを忘れてしまったようです。 –

関連する問題