簡體   English   中英

從JSON文件中讀取帶有html標記的內容並在javascript中顯示

[英]Reading the content with html tags from JSON file and displaying in javascript

我有JSON文件,其中包含要在移動應用程序中顯示的標簽和內容。

我正在使用$ .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."

如果我嘗試在javascript中打印CONSENT_EMAIL_REQUEST_BODY的值,它仍會顯示

  Please read the below content <br> Welcome to the project <br> you can now use the application.

它不是逐行顯示中斷
正在按原樣顯示。 如何替換這些html字符以在手機/瀏覽器中正確顯示。

在打印之前,您必須更換換行符
與'\\ n'。

假設如果將內容帶到變量mycontent,則可以按以下方式進行操作:

mycontent = mycontent.replace(/<br\s*\/?>/mg,"\n");

然后打印mycontent。 而已。

它對我有用以下代碼:

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);

也許您使用的是.text()而不是.html()

演示小提琴: http : //jsfiddle.net/lparcerisa/q3hmjg7w/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM