【问题标题】:jQuery works in console but when I save it in my site it doesn'tjQuery 在控制台中工作,但是当我将它保存在我的网站中时,它没有
【发布时间】:2020-12-21 14:36:42
【问题描述】:

创建了这个 jQuery 以在我的 wordpress 网站中添加额外的价格:

jQuery(document).ready( function(){
    var totprice = jQuery(".order-total .woocommerce-Price-amount.amount").text().replace('$','').replace(',','');
    var leaseprice = (Number(totprice)) * (0.0198);
    jQuery(".woocommerce-checkout-review-order-table tbody").append("<tr class='lp-total'><td class='lp-text'>Estimated Commercial Lease Total: </td>" + "<td class='lp-price'>" + "$" + leaseprice.toFixed(1) + "</td>"+ "</tr>");
});

html:

<table class="shop_table woocommerce-checkout-review-order-table">
    <thead>
        <tr>
            <th class="product-name">Product</th>
            <th class="product-total">Subtotal</th>
        </tr>
    </thead>
    <tbody>
        <tr class="cart_item">
            <td class="product-name"> Max-EV 20&nbsp; <strong class="product-quantity">×&nbsp;1</strong> </td>
            <td class="product-total"> <span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>30,950.00</span>
            </td>
        </tr>
    </tbody>
    <tfoot>
        <tr class="cart-subtotal">
            <th>Subtotal</th>
            <td><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>30,950.00</span>
            </td>
        </tr>
        <tr class="order-total">
            <th>Total</th>
            <td><strong><span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">$</span>30,950.00</span></strong> </td>
        </tr>
    </tfoot>
</table>

它在控制台中工作得很好,但是一旦我将它保存在 custom.js 文件中并按下刷新按钮以检查它是否工作,我会在 jQuery 中看到 html 一秒钟然后它会自动隐藏。

请注意,如果我将 html 附加到同一页面上的其他任何位置,它可以正常工作。但是在这里我试图将它附加到一个标签中,当我这样做时,就会出现这个问题。

jQuery 代码有问题吗?

【问题讨论】:

    标签: javascript jquery wordpress woocommerce checkout


    【解决方案1】:

    你在你的 JS 中加入了 jQuery 吗?

    var script = document.createElement('script');
    script.src = '//code.jquery.com/jquery-3.4.1.min.js';
    script.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(script);
    

    同时尝试清除缓存。

    【讨论】:

    • 是的,包含 jQuery。如果我将 html 附加到网站的其他任何地方,它就可以正常工作。但在这里我试图将它附加到 标记中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    • 2013-05-08
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多