jQuery:单击更改div文本,将无法正常工作

时间:2019-06-30 11:29:07

标签: javascript jquery

这是我的代码:

    var woocommerce_price_displayed = $('.product_intial_price').find('.woocommerce-Price-amount').text();

        $(".yearly-package").click(function () {
        var yearprice = $('#year_price').text();
    $('.electro-price').find('.woocommerce-Price-amount').text(function () {
    return $(this).text().replace(woocommerce_price_displayed, yearprice); 
});
    })

    $(".halfyear-package").click(function () {
       var halfyearprice = $('#halfyear_price').text();
$('.electro-price').find('.woocommerce-Price-amount').text(function () {
    return $(this).text().replace(woocommerce_price_displayed, halfyearprice); 
});
    })

    $(".single-package").click(function () {
      var oneprice = $('#one_price').text();
$('.electro-price').find('.woocommerce-Price-amount').text(function () {
    return $(this).text().replace(woocommerce_price_displayed, oneprice); 
});
    })


单击div时,价格将改变。但是,如果我再次单击其他按钮-价格将不会再次更改。

怎么了?为什么只能在首次点击时使用?

这基本上是每个包装的html:

<div class="ps-package single-package first-pack" data-val="1">
<div class="ps-name">Single Package</div>
<div id="one_price" class="ps-price">$99</div>

</div>

<span class="electro-price"><ins><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>99.00</span></ins> <del><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>150.00</span></del></span>

当我为每个这样的函数添加var woocommerce_price_displayed = oneprice;时:

    $(".halfyear-package").click(function () {
       var halfyearprice = $('#halfyear_price').text();
$('.electro-price').find('.woocommerce-Price-amount').text(function () {
    return $(this).text().replace(woocommerce_price_displayed, halfyearprice); 
    woocommerce_price_displayed = halfyearprice;
});

它根本无法工作,甚至一次也不会!为什么?

0 个答案:

没有答案