admin管理员组文章数量:1410737
There is a requirement that retrieving some msg including HTML tag from the back-end, how to rightly display HTML tag but not original tag character.
msg like below ( displays in quotation marks ) :
"This is a <a href="#">link</a>, for a test."
right display:
"This is a link, for a test."
There is a requirement that retrieving some msg including HTML tag from the back-end, how to rightly display HTML tag but not original tag character.
msg like below ( displays in quotation marks ) :
"This is a <a href="#">link</a>, for a test."
right display:
"This is a link, for a test."
Share Improve this question asked Mar 29, 2019 at 4:57 licaomenglicaomeng 9472 gold badges14 silver badges28 bronze badges 6- 2 Can you please show the snippet. – Maheer Ali Commented Mar 29, 2019 at 4:58
- 4 What exactly is the problem? – Aniket G Commented Mar 29, 2019 at 4:58
- 1 You have to bypass or escape the special characters.Here it is double quotes. Add a slash \ before it and you should be fine. – Krishna Prashatt Commented Mar 29, 2019 at 4:59
- What did you try? What gone wrong? – Kaiido Commented Mar 29, 2019 at 5:06
- @Francisaskquestion to attain what? There is simply no way to attain what it is to be attained. Proof: you are the only one who understood they wanted to keep the markup unparsed. – Kaiido Commented Mar 29, 2019 at 5:10
4 Answers
Reset to default 1It seems you want display string as html
. You just need to add it as innerHTML
. Not innerText
or textContent
Wrong Display
let str = `This is a <a href="#">link</a>, for a test.`
document.body.innerText += str;
Right Display
let str = `This is a <a href="#">link</a>, for a test.`
document.body.innerHTML += str;
If you want quotes around string use Template Strings
let str = `"This is a <a href="#">link</a>, for a test."`
document.body.innerHTML += str;
You can render HTML using document.write()
document.write(`This is a <a href="#">link</a>, for a test.`);`
But to append existing HTML string, you need to get the id of the node/tag under which you want to insert your HTML string.
There are two ways by which you can possibly achieve this:
Using DOM -
var tag_id = document.getElementById('tagid');
var newNode = document.createElement('p');
newNode.appendChild(document.createTextNode(`This is a <a href="#">link</a>, for a test.`));
node.appendChild(newNode);
Using innerHTML -
var tag_id = document.getElementById('tagid');
tag_id.innerHTML(`This is a <a href="#">link</a>, for a test.`);
Also You can use jQuery html parser to do that like this:
var html = `This is a <a href="#">link</a>, for a test.`;
html = $.parseHTML( html);
$("#tagId").append(html);
I hope this would help you. Thanks
Escaping -
You should escape any quotes found within your string. Place the \
in front of them to escape.
console.log("This is my string with quotes \" \" ")
You can use innerHTML
for this.
Example:
document.getElementById(id).innerHTML = yourTextWithHtml
本文标签: javascriptHow to display html tag in stringStack Overflow
版权声明:本文标题:javascript - How to display html tag in string? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745032797a2638599.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论