2017-01-30 97 views
1

我想逗號後格式化數的價格,用較小的字體: enter image description here格式編號價格樣式JS

例如,我有這樣的跨度:

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

我需要的東西,如更換:

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

我該如何使用javascript,php或css?

+1

一些建議:歐元符號必須在值之前。 –

+0

^^這取決於文化。 –

+0

這取決於語言。用英文寫42歐元;法語和意大利語是42歐元。 – bfontaine

回答

2

請在下面的代碼片段中找到我們可以使用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>

+2

如果您對任何人的答案進行了投票,請留下一些意見 – Curiousdev

+0

這對我有用。嗯,你可以使用preg_replace()函數用php代碼編輯你的答案嗎? –

-3

你可以這樣做在CSS中,並不難

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

1

試試這個:

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>

0

你可以使用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>