简体   繁体   English

如何将带有HTML标签的字符串打印为HTML

[英]How to print a string with HTML tags as HTML

I'm working on a small project where I grab JSON data from a url and parse it. 我正在一个小项目中,我从URL中获取JSON数据并进行解析。 All that is fine, the issue comes when I try to print the results to my HTML page. 很好,当我尝试将结果打印到HTML页面时,问题就来了。 I have the data I need, and it comes with included HTML tags, so I'd just like to stick it on my page and be on my day. 我拥有所需的数据,并且随附了HTML标记,因此,我只想将其粘贴在页面上并保持良好状态。 Every attempt below shows the result as 下面的每次尝试都将结果显示为

<ul> <li>Place this item in the&nbsp;<strong>Blue Bin</strong>.</li> </ul>

on my HTML page. 在我的HTML页面上。 It should be an unordered list with list items. 它应该是带有列表项的无序列表。 I shouldn't be seeing the tags. 我不应该看到标签。

I've tried: 我试过了:

var htmlString = $.parseHTML(data[1].body); $content.append( htmlString );

and: 和:

var htmlString = JSON.stringify(data[1].body); $("#ObjectId").html("<pre>" + htmlString + "</pre>");

I don't know what else to try. 我不知道还能尝试什么。 What am I missing? 我想念什么?

btw- data[1].body is the JSON data, it contains the first line of code above. btw- data [1] .body是JSON数据,它包含上面代码的第一行。

You can use innerHTML 您可以使用innerHTML

 let temp =`<ul> <li>Place this item in the&nbsp;<strong>Blue Bin</strong>.</li> </ul>` document.getElementById('test').innerHTML= temp 
 <div id='test'></div> 

You could add an id to the HTML element, and then add it as innerHTML to that element with document.getElementById(my-id).innerHTML = '<p>your string</p> 您可以将id添加到HTML元素,然后使用document.getElementById(my-id).innerHTML = '<p>your string</p>将其作为innerHTML添加到该元素。

more info! 更多信息!

This will get your job done. 这样可以完成您的工作。

<p>Hello World</p>

$('p').html('<b>Hi</b>');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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