2016-05-25 121 views
1

我需要爲每次調用後得到的結果生成一個HTML塊。下面是模板:使用JavaScript替換html的字符串

var tempScore = '<tr class="brick">' + 
    '<div class="brick" style="border: solid 1px #808080; padding: 5px; margin: 10px; overflow-y: auto; width: 100%">' + 
    '<h5 class="spamHead">{title}</h5>' + 
    '<div style="width: 120px;">' + 
    '<h4>{spamStatus}' + 
    '<div class="spamStat" style="margin-right: 5px; float: left; background: {color}">' + 
    '</div>' + 
    '</h4>' + 
    '</div>' + 
    '<h4 style="text-align: left;">{scoreInject}</h4>' + 
    '<div class="spamHeader"><h4>{spamHeaders}</h4></div>' + 
    '</div>' + 
    '</tr>'; 

目前,我遍歷,可替代所有的括號內{}項目沒有任何麻煩。但現在我需要添加一個新的帶有JavaScript的span標籤,並且沒有被正確替換。下面是我可以取代結果的可能項目之一:

tempScore.replace("{scoreInject}", '\<span class=\"tooltip\" onmouseover=\"tooltip.pop(this, \'Outlook' + 
         ' utilizes a self-learning filter to determine what you think is spam. While this is great for individual' + 
         ' users, it’s not consistent nor reliable for use across thousands of tests on our servers. Instead, we’ve ' + 
         'added in hundreds of spam rules that have been published by Outlook. Whenever the content in your campaign ' + 
         'triggers one of these rules, we’ll provide you with feedback on what can be changed to make your email look ' + 
         'less spammy to Outlook. This filter on Litmus uses built-in junk email filter for Outlook, which ships as ' + 
         'part of Microsoft Office. This has various sensitivity settings, here we have set it to ‘High’. The Microsoft ' + 
         'Outlook filter scores from 0-10 on the High sensitivity rating, with 0 being the highest (passing) and 10 being ' + 
         'the lowest (failing). Outlook rates an email with a 6.0 or higher (out of 10) as a failure. A lower score (lower than 6.0) ' + 
         'is considered a passing score with the High sensitivity rating.\', {position:0})"\>Spam Score: {spamScore}\</span\>'); 

我假設我弄亂了其中一個轉義字符。理想情況下,span標籤及其中的所有內容將整齊地替換爲{scoreInject}。由於該過程的性質,我不能輕鬆地爲每個項目分配id,並使用jQuery替換標記的屬性。

+0

嘗試更換蜱'\''左右'\'高\''用不同的字符(任何字符,就目前而言,像'*' ),看看是否可以解決這個問題......然後你可以用等價的標記/分隔符替代tick - 與'we''''相同 - 等等。 – ochi

+1

Incase你還沒碰過它,你可能想要考慮尋找類似於Handlebars JS for HTML模板(http://handlebarsjs.com/)的東西,這樣可以避免像這樣的笨拙的內聯HTML字符串 – Shakespeare

回答

2

發了的jsfiddle你:https://jsfiddle.net/eqymm0La/

我覺得你並不需要花葶\<\/span\>\"

此外,您還需要更新您的VAR:

tempScore = tempScore.replace("{scoreInject}", '<span class="tooltip" onmouseover="tooltip.pop(this, \'Outlook' + 
        ' utilizes a self-learning filter to determine what you think is spam. While this is great for individual' + 
        ' users, it’s not consistent nor reliable for use across thousands of tests on our servers. Instead, we’ve ' + 
        'added in hundreds of spam rules that have been published by Outlook. Whenever the content in your campaign ' + 
        'triggers one of these rules, we’ll provide you with feedback on what can be changed to make your email look ' + 
        'less spammy to Outlook. This filter on Litmus uses built-in junk email filter for Outlook, which ships as ' + 
        'part of Microsoft Office. This has various sensitivity settings, here we have set it to ‘High’. The Microsoft ' + 
        'Outlook filter scores from 0-10 on the High sensitivity rating, with 0 being the highest (passing) and 10 being ' + 
        'the lowest (failing). Outlook rates an email with a 6.0 or higher (out of 10) as a failure. A lower score (lower than 6.0) ' + 
        'is considered a passing score with the High sensitivity rating.\', {position:0})"\>Spam Score: {spamScore}</span>'); 
+0

確實,你不需要轉義'<或'''但是逃避它們是完全正確的。我相信唯一的問題是OP沒有分配將replace()'返回爲原始變量並將其設置爲innerHTML的結果。 [這裏是OP的字符串未被觸及的小提琴](https://jsfiddle.net/eqymm0La/1/) –

1

我不認爲你需要轉義除\'Outlookrating.\'之外的'字符。因此,請嘗試刪除您所做的其他所有轉義。

此外,爲了保持一致性,始終在代碼中使用相同類型的引號。您對"{scoreInject}"以外的所有內容使用了',因此您可能需要將其更改爲具有更簡潔的代碼。