簡體   English   中英

將HTML字符串附加到iframe中會導致語法錯誤,這樣做的正確方法是什么?

[英]Appending HTML strings into Iframe causing syntax errors, what is the correct way to do this?

我在做什么:

我正在存儲結構如下的(MySQL)格式文本:

I'm Robert (I'm = I am)<br>You're in France (You're = You are)<br><br><strong></strong><span class="st">?</span> <b>I</b>'m Robert  <br>? Am Robert <del> <br></del><br>? <b>You</b>'re Robert <br>? Are Robert  <br><br>I'm = I am<br>You're = ___ ___?<br><br><br><br>

我試圖從數據庫中檢索此字符串,然后將其附加到這樣的WYSIWYG編輯器中。

function enableEditMode(){
            card_content.document.designMode = 'On';
            $('iframe[name=card_content]').contents().find('body').append("I'm Robert (I'm = I am)<br>You're in France (You're = You are)<br><br><strong></strong><span class="st">?</span> <b>I</b>'m Robert  <br>? Am Robert <del> <br></del><br>? <b>You</b>'re Robert <br>? Are Robert  <br><br>I'm = I am<br>You're = ___ ___?<br><br><br><br>");
}

我收到以下錯誤:

參數列表后出現SyntaxError:缺少)

我認為這是因為引號引起問題。

題:

如何在不出現此類語法錯誤的情況下將HTML附加到DOM中?

只需更換

class="st"

通過

class='st'

應該可以。 我看不到更多的報價沖突。

我認為您需要轉義單引號或雙引號:

function addslashes( str ) {
    return (str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
}

來自: 用JavaScript轉義字符串

或者,您在服務器端對字符串進行轉義並返回轉義的字符串。 因此,結果應如下所示:

"this is a test, isn\'t it great! <span class=\"test\">Test</span>"

暫無
暫無

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

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