簡體   English   中英

如何在js中將html內容添加到靜態文本

[英]How to add html content to a static text in js

我想顯示js的靜態內容以及其中的圖像

content = {
    message: "The best city in the world?"
    answer:" Paris is one of the best city.'<img src ='assets/paris.gif alt="Smiley face" height="42" width="42">'".
}

我的HTML

 ng-bind-html= content.answer

當我運行代碼時,提示錯誤

Uncaught SyntaxError: Unexpected identifier

這是我嘗試過的。任何人都可以找到我的錯誤。謝謝。

嘗試這個:

content = {
    message: 'The best city in the world?',
    answer: 'Paris is one of the best city.<img src ="assets/paris.gif" alt="Smiley face" height="42" width="42">.'
}

您的代碼中存在一些問題。 可變內容應如下所示:

var content = {
    message: "The best city in the world?",
    answer: 'Paris is one of the best city. <img src ="assets/paris.gif" alt="Smiley face" height="42" width="42">'
}

因此,請在IMG標簽內使用雙引號,並在object屬性中使用單引號。 另一個問題是{{content.question}}毫無意義,因為內容內部只有“ message”和“ answer”屬性。 這樣就可以使用content.message

您的content字符串沒有轉義字符。 當您將字符串用雙引號引起來時,除非在字符串中使用轉義字符\\否則無法在字符串中再次使用它\\

content = {
    message: "The best city in the world?",
    answer: "Paris is one of the best city.'<img src =\"assets/paris.gif \" alt=\"Smiley face\" height=\"42\" width=\"42\">'"
}

如果您要在角度模板中使用它,因為您要呈現的答案中包含HTML ,請與ng-bind-html

暫無
暫無

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

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