[英]javascript set weight to characters in text
我想申請文本"<b>H</b>ell<b>o</b>"
與ID段落= “predogled”使用JavaScript,因此,輸出將是H ELL O操作 。
我嘗試了這個:
<script>
function formattext(){
document.getElementById('predogled').innerText="<b>H</b>ell<b>o</b>";
}
</script>
<p id="predogled"></p>
<button type="button" onclick="formattext()">Format!</button>
但是這段代碼從字面上打印出"<b>H</b>ell<b>o</b>"
並且這些字符不是我想要的粗體。
那就是innerText
所做的。 如果希望將其解釋為HTML,請改用innerHTML
。
您必須使用innerHTML
<script> function formattext(){ document.getElementById('predogled').innerHTML="<b>H</b>ell<b>o</b>"; } </script>
<p id="predogled"></p> <button type="button" onclick="formattext()">Format!</button>
使用innerText
您可以打印字符串。
使用innerHTML
可以打印元素的HTML內容。
嘗試使用除了innerText
之外的innerHTML
:
function formattext(){
document.getElementById('predogled').innerHTML="<b>H</b>ell<b>o</b>";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.