2014-08-27 87 views
0

我有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字符以在移動/瀏覽器中正確顯示。

回答

0

這是爲我工作用下面的代碼:也許您正在使用.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); 

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

1

在打印之前,您必須更換線路中斷
用 '\ n'。

假設,如果你把你的內容給一個變量爲myContent,那麼你可以做到這一點爲:

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

然後打印爲myContent。而已。