简体   繁体   English

用 Shopify 购物车页面上的 Javascript 替换文本

[英]Replacing Text with Javascript on Shopify Cart Page

In this store - https://commonfibers.com/cart - there's a private app that is creating line item properties on some products.在这家商店 - https://commonfibers.com/cart - 有一个私人应用程序正在某些产品上创建订单项属性。 Example - https://gyazo.com/aa86c0696b406e5c128df7640f353c7f .示例 - https://gyazo.com/aa86c0696b406e5c128df7640f353c7f

I want to clean up the display of the line item properties.我想清理订单项属性的显示。 So this text: Art: 'Art - Blank (e9bd8c48c4134298bb9013bb595d8c6b)' would instead look like this: Art: 'Art - Blank'因此,此文本:Art: 'Art - Blank (e9bd8c48c4134298bb9013bb595d8c6b)' 将改为:Art: 'Art - Blank'

I have created this.liquid file:我创建了 this.liquid 文件:

<script>

  document.addEventListener('DOMContentLoaded', function() {
    var divArt = document.getElementsByClassName('property-Art');
    var text = divArt.innerText;
    for (var i = 0; i < divArt.length; i++) {
      var text = divArt[i].innerText;
      console.log("Beginning text - " + text);
      text = text.replace(/\s*\(.*?\)\s*/g, '');
      console.log("Processed text - " + text);
      console.log("innerText - " + divArt[i].innerText);
    };
  }, false);

</script>

And its working in that it removes the unwanted content.它的工作原理是删除不需要的内容。 What I'm stuck on is how to insert the processed text back into the DOM so it shows up in the browser.我坚持的是如何将处理后的文本插入回 DOM 中,以便它显示在浏览器中。 I can see the replace logic works as I'm console.log it - https://gyazo.com/6f3aed206ccd08343dc50415c8dcae01 .我可以看到替换逻辑工作正常,因为我是 console.log 它 - https://gyazo.com/6f3aed206ccd08343dc50415c8dcae01 But my efforts to get it to display to the user have failed so far.但到目前为止,我想让它显示给用户的努力都失败了。

Thanks!谢谢!

You need to assign the result text back to something.您需要将结果text分配回某些内容。 Try adding this at the end of your function:尝试在 function 的末尾添加:

divArt[i].innerText = text;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM