請注意,這個問題已經有了答案這里,但答案是在jQuery中提供的,我需要用vanilla javascript來完成類似的事情。
下面是我的問題的細節。我有一個價格字串要在一個頁面中顯示,它將在html中顯示為<span>60.00</span>我想把這個字串轉換成這樣的東西<span>60,<sup>00</sup></span>;。
下面是我試過的代碼。
。 let value = "60.00" ;
value = value.replace(".", ",")。
let values = value.split(',') 。
var tag = document.createElement("sup") 。
var text = document.createTextNode(values[1])。
text = tag.appendChild(text)。
let text1 = JSON.stringify(text)。
alert(values[0] text1);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
如果我運行這段代碼,我得到60{}作為輸出。
如果我洗掉JSON.stringify(),我得到60[object Text]。
60,?這是想要的輸出。
PS:我想用angular中的管道來完成這個任務,這就是為什么我想用這種方式。如果有任何相關的管道,請讓我知道。
與其添加一個sup標簽,為什么不添加字串<sup></sup>來代替呢?
let span = document. querySelector("span") 。
let value = span.innerHTML;
const sup = `<sup> ${value. split('.')[1]}</sup> `;
value = `${value.split('.')[0]}, ${sup}`;
span.innerHTML = value;
<span>/span>60. 00</span>/span>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
let span = document. querySelector("span") 。
let value = span.innerHTML;
const sup = `<sup> ${value. split('.')[1]}</sup> `;
value = `${value.split('. ')[0]}.${sup}`;
span.innerHTML = value;
<span>/span>10. 00</span>/span>
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/306594.html
標籤:
下一篇:如何在文本框中同時添加文字和圖片
