首页 > 解决方案 > 如何更改 document.getClassName 调用中的所有项目值(没有它只使用第一个元素值)

问题描述

我正在尝试更改页面上的单个产品价格,因为由于我使用的电子商务平台,编码都搞砸了。我需要在一个 £ 符号上分开,这似乎适用于单个项目

我尝试通过使用 getElementID 来改变我这样做的方式,但由于电子商务平台的运作方式,这并不好,而且我无法更改任何后端 stff。

<div class="products-price">
    <!--START: ITEMPRICE-->
    <span class="subfix">[ITEMPRICE]</span>
    <!--END: ITEMPRICE--> 
</div>
jQuery(document).ready(function () {
    var total = document.getElementsByClassName('subfix')
    for (i = 0; i < total.length; i++) {
        total[i].textContent.split('£')[1];
    }
});

标签: javascripthtml

解决方案


您还必须将文本设置为该元素

jQuery(document).ready(function() {
  var total = document.getElementsByClassName('subfix')

  for (i = 0; i < total.length; i++) {
    total[i].textContent=total[i].textContent.split('£')[1];
  }



});
<div class="products-price">
  <span class="subfix"> </span>
</div>


推荐阅读