![](/img/trans.png)
[英]My javascript code doesn't work, but when it is executed in the console it works
[英]jQuery works in console but when I save it in my site it doesn't
創建此 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 <strong class="product-quantity">× 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 代碼有問題嗎?
您是否將 jQuery 包含到您的 JS 中?
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);
也嘗試清除緩存。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.