我需要爲每次調用後得到的結果生成一個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替換標記的屬性。
嘗試更換蜱'\''左右'\'高\''用不同的字符(任何字符,就目前而言,像'*' ),看看是否可以解決這個問題......然後你可以用等價的標記/分隔符替代tick - 與'we''''相同 - 等等。 – ochi
Incase你還沒碰過它,你可能想要考慮尋找類似於Handlebars JS for HTML模板(http://handlebarsjs.com/)的東西,這樣可以避免像這樣的笨拙的內聯HTML字符串 – Shakespeare