使用JSON文件中的html标签阅读内容并在javascript中显示
问题描述:
我有JSON文件,其中包含要在我的移动应用程序中显示的标签和内容。使用JSON文件中的html标签阅读内容并在javascript中显示
我使用$ .getJSON在本地项目中读取JSON文件。它将返回JSON对象。使用该对象,我在Javascript中显示标签/文本内容。在显示带有html字符的内容时,它不能正确加载,仍然是它的显示html字符。
里面的内容JSON
"CONSENT_EMAIL_REQUEST_BODY" : "Please read the below content <br> Welcome to the project <br> you can now use the application."
如果我尝试打印CONSENT_EMAIL_REQUEST_BODY的值在JavaScript它仍然显示
Please read the below content <br> Welcome to the project <br> you can now use the application.
它不是由线显示线突破
都显示,因为它是。如何替换这些html字符以在移动/浏览器中正确显示。
答
这是为我工作用下面的代码:也许您正在使用.text()
代替.html()
var json = jQuery.parseJSON('{"CONSENT_EMAIL_REQUEST_BODY" : "Please read the below content <br> Welcome to the project <br> you can now use the application."}');
$("span").html(json.CONSENT_EMAIL_REQUEST_BODY);
?
答
在打印之前,您必须更换线路中断
用 '\ n'。
假设,如果你把你的内容给一个变量为myContent,那么你可以做到这一点为:
mycontent = mycontent.replace(/<br\s*\/?>/mg,"\n");
然后打印为myContent。而已。