【发布时间】:2023-04-10 21:31:01
【问题描述】:
预订插件将价格显示为带 2 位小数的数字。 由于我找不到要连接的过滤器,我想我可能需要一个 JS 循环。
页面上有几处房产的价格为 123.45 美元。我想把它四舍五入到 123 美元
价格的html是:
<span class="price"><span class="currency">$</span>"123,45<span>
我尝试了一个循环,但我不是很擅长 JS:
function roundPrice() {
let oldPrices = document.getElementsByClassName("price");
for (let i = 0; i < oldPrices.length; i++) {
// strip $ of price
oldPrices[i] = oldPrices[i].substr[1];
// string to num
oldPrices[i] = parseInt(oldPrices[i]);
// round number
let newPrice = Math.round(oldPrices[i]);
return oldPrices[i] = newPrice;
}
}
roundPrice();
但这不起作用。有这样的解决方案吗?
非常感谢,山姆
【问题讨论】:
-
嗨,在什么意义上它不起作用?您是否更改了任何输出,或者系统是否崩溃,或者您是否在 console.log 中看到任何错误。由于这是 Wordpress,您能告诉我们您在哪里运行此代码/将其挂钩?谢谢。
-
我没有得到任何输出。但是在 WP 中你总是必须使用钩子吗?该函数是否没有被调用?这可能是问题所在,我找不到这个钩子。
-
你把你的代码放在哪里了 - 在functions.php/插件中......?
-
在一个文件 custom-js.js 中,我把它挂在了 wp_enqueue_scripts 的 functions.php 中
-
您可以发布您的入队电话以便我们看看吗?可能是您的代码被调用但有点早。它是什么预订插件?
标签: javascript arrays wordpress filter rounding