我们可以在javascript中将字符串转换为html吗?

时间:2018-01-11 08:17:49

标签: javascript

我可以在javascript中将字符串转换为HTML吗?

let description = "<a className="peoplelink" id="ether">Ether</a> // is render as 'the historian of the'";

     let html= // I know that it is easy to do with jquery by using .html() method, but can we convert a string to HTML in javascript?

1 个答案:

答案 0 :(得分:1)

无需转换,是一个简单的字符串到innerHtml:

即:

const markup = `
 <div class="person">
    <h2>
        ${person.name}
    </h2>
    <p class="location">${person.location}</p>
    <p class="bio">${person.bio}</p>
 </div>
`;

document.body.innerHTML = markup;