替换加载时不在DOM中的文本中的句点为逗号。

huangapple go评论52阅读模式
英文:

Replace dot with comma of text which is not in DOM while loading

问题

需要修改WordPress主题中计数器动画的输出。页面加载时有一个空的span,如下所示:

<span class="counter"></span>

动画完成后,span看起来像这样:

<span class="counter">30.5</span>

由于这是一个德语网站,输出需要使用逗号而不是点,所以它应该看起来像这样:30,5
如何实现这一点?

阅读类似的帖子,看起来我需要一个WebKitMutationObserver(?),但我没有使用它的经验。感谢任何帮助!

英文:

I'm using a WordPress Theme and need to modify the output of a counter animation.
While the page is loading, there is an empty span like this:

&lt;span class=&quot;counter&quot;&gt;&lt;/span&gt;

After the animation is finished, the span looks like this:

&lt;span class=&quot;counter&quot;&gt;30.5&lt;/span&gt;

Since it is a german website the output needs to be with a comma instead of a dot, so it should look like this: 30,5
How can I achieve this?

Reading similar posts it looks like I need a WebKitMutationObserver (?) but I have no experience using it. Thanks for any help!

答案1

得分: 1

以下是您要翻译的内容:

const elCounter = $('#counter');

$('button').on('click', () => {
  $({progress: 0}).animate(
    {progress: 1000}, {
    duration: 30000,
    step: val =>  {
      elCounter.text((val / 10).toFixed(2));
    }}
  )
});

new MutationObserver(() => {
  const replacement = elCounter.text().replace('.', ',');
  
  if(elCounter.text() !== replacement){
    elCounter.text(replacement);
  }
})
.observe(elCounter[0], { childList: true });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="counter">0</div>
<button>Go progress</button>
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const elCounter = $(&#39;#counter&#39;);

$(&#39;button&#39;).on(&#39;click&#39;, () =&gt; {
  $({progress: 0}).animate(
    {progress: 1000}, {
    duration: 30000,
    step: val =&gt;  {
      elCounter.text((val / 10).toFixed(2));
    }}
  )
});

new MutationObserver(() =&gt; {
  const replacement = elCounter.text().replace(&#39;.&#39;, &#39;,&#39;);
  
  if(elCounter.text() !== replacement){
    elCounter.text(replacement);
  }
})
.observe(elCounter[0], { childList: true });

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;counter&quot;&gt;0&lt;/div&gt;
&lt;button&gt;Go progress&lt;/button&gt;

<!-- end snippet -->

答案2

得分: 0

使用 value.toLocaleString('en-de', {...}) 将解决您的需求。

const counterElement = document.querySelector('.counter');
const value = parseFloat(counterElement.textContent);
const formattedValue = value.toLocaleString('en-de', {
  minimumFractionDigits: 1,
  maximumFractionDigits: 1
});
counterElement.textContent = formattedValue;
<span class="counter">30.5</span>
英文:

Using value.toLocaleString(&#39;en-de&#39;,{...}) will solve your purpose

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const counterElement = document.querySelector(&#39;.counter&#39;);
const value = parseFloat(counterElement.textContent);
const formattedValue = value.toLocaleString(&#39;en-de&#39;, {
  minimumFractionDigits: 1,
  maximumFractionDigits: 1
});
counterElement.textContent = formattedValue;

<!-- language: lang-html -->

&lt;span class=&quot;counter&quot;&gt;30.5&lt;/span&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月18日 03:15:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定