简体   繁体   English

如何替换 div 中的特定字符?

[英]How can I replace specific characters in a div?

I would like to change the comma into an apostrophe in this div.我想在这个 div 中将逗号更改为撇号。

<div id="range" class="range-slider">   
  <div class="something">15,000.00</div>
</div> 

So instead of 15,000.00 I would like to do 15'000.00所以不是 15,000.00 我想做 15'000.00

I cannot alter the HTML so I think I need to achieve this in JS, right?我无法更改 HTML 所以我想我需要在 JS 中实现这一点,对吧?

Do I need to use the changeText function?我需要使用changeText function吗?

Many thanks in advance.提前谢谢了。

There is no native changeText function in JavaScript. JavaScript 中没有原生changeText function。 You can change the text content of an element using textContent property and use replace() method to replace , to ' .您可以使用textContent属性更改元素的文本内容,并使用replace()方法将,替换为'

 let elem = document.querySelector(".something") elem.textContent = elem.textContent.replace(",", "'");
 <div id="range" class="range-slider"> <div class="something">15,000.00</div> </div>

To add to TechySharnav 's answer, you can also query for the classname directly.要添加到TechySharnav的答案,您还可以直接查询类名。

var el = documents.getElementByClassNames("something");
el.textContent = el.textContent.replace(',',"'");

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

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