简体   繁体   中英

Convert string to html tags in Javascript

I want to convert the following string to HTML tags and place it inside my div.

<strong>asdfadfsafsd</strong>

I am using the following code to place it inside my div:

var message = "<strong>testmessage</strong&gt";
document.getElementById('message').innerHTML = bericht;

The problem is that I see the following in my div now:

<strong>testmessage</strong>

But I want to see: testmessage

What is the problem?

 var string = "&lt;strong&gt;asdfadfsafsd&lt;/strong&gt;", results = document.getElementById("results") results.innerHTML = string; results.innerHTML = results.textContent; 
 <div id="results"></div> 

At first load the it as html. Then fetch it as text and then again load it as HTML :)

Refer HTML Entities

Try createElement

var tag = document.createElement("strong");
var t = document.createTextNode("testmessage");
tag.appendChild(t);
document.body.appendChild(tag);

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