简体   繁体   中英

Javascript: how to add < as text of label html

I have a label, and i change the text in this label using this code:

let label = document.getElementById("resultLabel")
label.innerHTML = stringResult;

where stringResult is a string.

It works, but if the string contains "<" the text is truncated at that point: for example if the string is "a<b" only "a" will be shown.

How can I insert a string containing "<" as the text of an HTML label?

How about using .textContent ?

let label = document.getElementById("resultLabel");
label.textContent = stringResult;

You can encode it with &lt; like this :

a&lt;b

Escape it \\< .

 let label = document.getElementById("resultLabel") label.innerHTML = 'a \\< b';
 <label id='resultLabel'></label>

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.

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