使用jQuery删除额外的破折号

时间:2015-01-03 19:25:43

标签: jquery wordpress woocommerce

这是我的情况。我正在通过他们的Woocommerce(WordPress)网站帮助客户。我一直在使用jquery隐藏更高的变化价格,只显示更低的价格。有" - "仍然显示,我想用jQuery删除它。我已经尝试了几个小时而没有成功。帮助将不胜感激。

这是我的HTML代码:

<span class="price"><del><span class="amount">$13.29</span>–<span class="amount">$332.25</span>

</del> <ins><span class="amount">$10.63</span>–<span class="amount">$151.84</span></ins>

</span>
<div class="product-meta-wrapper">

<h3 class="heading-title product-title"><a href="http://sproutman.com/shop/product/beginners-dozen-seeds/">Beginner’s Dozen Sprouting Seeds</a></h3>

<div class="second-rating"></div>   <span class="price"><del><span class="amount">$99.92</span>

</del> <ins><span class="amount">$87.89</span></ins>

</span>
<div class="list_add_to_cart"><a href="/product-category/organic-sprouting-seeds/recommendations-for-beginners/?add-to-cart=650" rel="nofollow" data-product_id="650" data-product_sku="SPRTSAMP" class="button add_to_cart_button product_type_simple">Add to cart</a>

</div>
</div>

我的jquery代码:

$(document).ready(function () {
var firstHighPrice = $('del span:nth-child(2)');
var secondHighPrice = $('ins span:nth-child(2)');
firstHighPrice.hide();
secondHighPrice.hide();

});

链接到JSFiddle:http://jsfiddle.net/a5Lyxsur/2/

3 个答案:

答案 0 :(得分:1)

如果你知道你想要删除之前的文本是-,那么只需使用previousSibling来删除文本节点:

firstHighPrice.hide();
$(firstHighPrice[0].previousSibling).remove();

secondHighPrice.hide();
$(secondHighPrice[0].previousSibling).remove();

演示:http://jsfiddle.net/a5Lyxsur/3/

答案 1 :(得分:1)

如果您将<span></span>标记放在“ - ”下面,如下所示

<span class="price">
    <del>
        <span class="amount">$13.29</span>
        <span>–<span>
        <span class="amount">$332.25</span>
   </del> 

然后修改选择器,如下所示

$(document).ready(function () {
    var firstHighPrice = $('del span:nth-child(2), del span:nth-child(3)');
    var secondHighPrice = $('ins span:nth-child(2), ins span:nth-child(3)');
    firstHighPrice.hide();
    secondHighPrice.hide();
});

工作样本位于http://jsfiddle.net/a5Lyxsur/4/

答案 2 :(得分:0)

在此each函数中,span classprice方法应用于每个del insspan元素及其中的empty元素。然后$('span.price').each(function () { var delSpans = $(this).find('del span').detach(); $(this).find('del').empty(); $(this).find('del').append(delSpans); var insSpans = $(this).find('ins span').detach(); $(this).find('ins').empty(); $(this).find('ins').append(insSpans); }); var firstHighPrice = $('del span:nth-child(2)'); var secondHighPrice = $('ins span:nth-child(2)'); firstHighPrice.hide(); secondHighPrice.hide(); 方法用于删除短划线( - )。最后,再次附加先前分离的元素。 detach

{{1}}