I want to apply text "<b>H</b>ell<b>o</b>"
to the paragraph with id="predogled" using javascript, so the output will be H ell o .
I tried with this:
<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>
But this code literally print "<b>H</b>ell<b>o</b>"
and the characters are not bold as I want.
That's what innerText
does. If you want it to be interpreted as HTML, use innerHTML
instead.
you have to use 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>
With innerText
you print a string.
With innerHTML
you print the HTML content of the elements.
Try using innerHTML
besides innerText
:
function formattext(){
document.getElementById('predogled').innerHTML="<b>H</b>ell<b>o</b>";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.