简体   繁体   English

使用js格式化数字样式样式

[英]Format number price styles with js

I want format a number price after comma, with an smaller font: 我希望格式化逗号之后的数字价格,字体较小: 在此输入图像描述

For example, I have this span: 例如,我有这个范围:

<span class="price">9,95€</span>

I need replace with something like: 我需要更换以下内容:

<span class="price">9,<span class="small-price">95€</span></span>

How can I do it with javascript, php, or css? 我怎么能用javascript,php或css做到这一点?

Please find below snippet we can do this using regex . 请在下面的代码中找到我们可以使用regex执行此操作。

 $.each($('.price'), function() { var price = $(this).html(); $(this).html(price.replace(/(\\d*\\,)(\\d*)(\\D*)/, '<span style="font-size:22px;">$1</span><span style="font-size:16px;">$2</span><span style="font-size:14px;">$3</span>')); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='price'>270,30€</div> 

Try this: 尝试这个:

 var prices = document.getElementsByClassName('price'); for (var i = 0; i < prices.length; i++) { var priceEl = prices[i]; var price = priceEl.innerHTML; var priceParts = price.split(','); var smallPriceValue = priceParts[1]; if (smallPriceValue) { var span = document.createElement('span'); span.className = "small-price"; span.innerHTML = smallPriceValue; priceEl.innerHTML = priceParts[0]+ ","; priceEl.appendChild(span); } } 
 .price { font-size: 18px; } .small-price { font-size: 12px; } 
 <span class="price">9,95€</span><br> <span class="price">12,85€</span> 

You could use document.getElementsByClassName and change the element with plain Javascript. 您可以使用document.getElementsByClassName并使用纯Javascript更改元素。

 [].forEach.call(document.getElementsByClassName('price'), function (a) { a.innerHTML = a.innerHTML.replace(/(\\d\\d€)/, '<span class="small-price">$1</span>'); }); 
 .price { font-size: 36px; } .small-price { font-size: 24px; } 
 <span class="price">9,95€</span><br> <span class="price">0,01€</span> 

You can do this in CSS, not that difficult 你可以在CSS中做到这一点,而不是那么困难

 .small-price { font-size: 7pt; } 
 5,<font class="small-price">95</font> 

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

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