I need to set a class if the string in the element below has a value higher than 0.
如果下面元素中的字符串的值大于0,我需要设置一个类。
The script works if I remove the strong part and the colon, but I can´t control this so how do i targets this?
如果我删除强部分和冒号,该脚本可以工作,但是我无法控制这个,所以我该如何定位?
Script:
$('.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>
4 个解决方案
#1
1
Use .textContent
to get the text without any HTML tags. And remove everything except the digits before converting to a number
使用.textContent来获取没有任何HTML标记的文本。并且在转换为数字之前删除除数字之外的所有内容
$('.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>
#2
1
Here is another solution:
这是另一个解决方案:
$('.stockinfo.col4').filter(function(index){
var number=$(this).find('strong')[0].nextSibling.nodeValue.replace(/[^0-9]/g, '');
return parseInt(number) > 0;
}).addClass("instocktest");
Working fiddle
#3
0
Use regex to match if there is more than one 0 in content:
如果内容中有多个0,请使用正则表达式进行匹配:
$(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>
#4
0
Try this, you can check if text()
yields content in your div
试试这个,你可以检查text()是否会在div中产生内容
$('.stockinfo.col4').filter(function(index){
return parseInt($(this).text()) > 0;
}).addClass("instocktest");
#1
1
Use .textContent
to get the text without any HTML tags. And remove everything except the digits before converting to a number
使用.textContent来获取没有任何HTML标记的文本。并且在转换为数字之前删除除数字之外的所有内容
$('.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>
#2
1
Here is another solution:
这是另一个解决方案:
$('.stockinfo.col4').filter(function(index){
var number=$(this).find('strong')[0].nextSibling.nodeValue.replace(/[^0-9]/g, '');
return parseInt(number) > 0;
}).addClass("instocktest");
Working fiddle
#3
0
Use regex to match if there is more than one 0 in content:
如果内容中有多个0,请使用正则表达式进行匹配:
$(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>
#4
0
Try this, you can check if text()
yields content in your div
试试这个,你可以检查text()是否会在div中产生内容
$('.stockinfo.col4').filter(function(index){
return parseInt($(this).text()) > 0;
}).addClass("instocktest");