检查span是否包含值,但值是否在另一个嵌套范围之后

时间:2017-12-08 06:17:33

标签: javascript jquery html wordpress

尝试检查span .woocommerce-Price-amount是否包含19.76。我认为它没有用,因为在我试图定位的值之前有<span class="woocommerce-Price-currencySymbol">$</span>

如何忽略currencySymbol范围?

jQuery(document).ready(function($) {
  $(".ivpa_term").click(function() {
    setTimeout(function() {
      $(".woocommerce-Price-amount").each(function() {
        var inner = $(this).text();
        if (!isNaN(inner) && 19.75 < inner && inner <= 19.76) {
          console.log("Exists!");
        }
      });
    }, 0001);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<span class="quantity">2 × 
  <del>
    <span class="woocommerce-Price-amount amount">
      <span class="woocommerce-Price-currencySymbol">$</span> 21.95
    </span>
  </del>
  <span class="woocommerce-Price-amount amount">
    <span class="woocommerce-Price-currencySymbol">$</span>19.76
  </span>
</span>

5 个答案:

答案 0 :(得分:1)

我不是正则表达式专家,但你可以尝试这个,如果你只想删除美元并获得数字

jQuery(document).ready(function( $ ) {

$( ".ivpa_term" ).click(function() {
     setTimeout(function() {

  $(".woocommerce-Price-amount").each(function () {
        var inner = $(this).text();
        var numb = inner.match(/\d+\.+\d+/g); //CHECK FOR NUMBERS AND DECIMALS
        numb = numb.join(""); 
        if (!isNaN(numb) && 19.75 < numb && numb <= 19.76){
                            console.log("Exists!");
        }
        });
                }, 0001);
});
});

答案 1 :(得分:1)

对于特定值,您可以使用:contains选择器

&#13;
&#13;
var res= $(".woocommerce-Price-amount:contains(19.76)").css('color', 'red');
if(res.length){
   console.log('Exists');
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<span class="quantity">2 × <del>
<span class="woocommerce-Price-amount amount">
<span class="woocommerce-Price-currencySymbol">$</span>21.95</span>
</del> 
<span class="woocommerce-Price-amount amount">
<span class="woocommerce-Price-currencySymbol">$</span>19.76</span>
</span>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以通过多种方式达到该值,其中一种可能是

for(int i = 0; i < 256; ++i) hash[i] = 0;  // Before entering while reset hash
                                           // counters for sliding window
while (right < s.length()) {
    if (hash[s.charAt(right)] == 0) {  // Use hash here for counting frequency
        count++;
}
hash[s.charAt(right)]--;  // Consistently increment for right and decrement for left

<强>解释

  • 获取元素 var textNode = Array.from(this.childNodes).filter(el => el.nodeType == 3 && Number(el.nodeValue) > 0 ); var inner = $(textNode[0]).text().trim();
  • 的所有childNodes
  • 过滤 textnodes 数值大于0

<强>演示

&#13;
&#13;
.woocommerce-Price-amount
&#13;
      $(".woocommerce-Price-amount").each(function() {
        var textNode = Array.from(this.childNodes).filter(el => el.nodeType == 3 && Number(el.nodeValue) > 0 );
        //console.log(textNode);
        var inner = $(textNode[0]).text().trim();
        console.log(inner);
        if (!isNaN(inner) && 19.75 < inner && inner <= 19.76) {
          console.log("Exists!");
        }
      });
&#13;
&#13;
&#13;

答案 3 :(得分:0)

您可以先替换货币符号,然后使用parseFloat()

将剩余内容转换为浮点数

jQuery(document).ready(function($) {
  $(".woocommerce-Price-amount").each(function() {
    var inner = parseFloat($(this).text().replace("$", ""));
    if (!isNaN(inner) && 19.75 < inner && inner <= 19.76) {
      console.log("Exists!");
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="quantity">2 × 
  <del>
    <span class="woocommerce-Price-amount amount">
      <span class="woocommerce-Price-currencySymbol">$</span> 21.95
    </span>
  </del>
  <span class="woocommerce-Price-amount amount">
    <span class="woocommerce-Price-currencySymbol">$</span>19.76
    </span>
  <span class="my-price"></span>
</span>

答案 4 :(得分:0)

只需替换代码,

var inner = $(this).text();

var inner = $(this).contents().eq(2).text();

适合你。