我從數據庫中獲得一些降價文本。我使用Showdown.js這種降價轉換爲HTML:字符串到DOM到字符串
var showdown = new Showdown.converter();
var str = showdown.makeHtml(myDatabaseString);
當有嵌入在降價中的代碼,Showdown.js將很好地把它包裝成<pre><code>
標籤,所以str
可能是這個樣子:
<p>Some text bla</p><pre><code>Some code</pre></code><p>Text again</p>
現在我想通過使用SyntaxHighlighter突出顯示代碼段的語法來使事情變得更漂亮。重要的是,最終我會得到一個包含之前所有內容的字符串,以及用於格式化的附加HTML。
我的這種做法是JQuery的操作DOM能力:
$(str).each(function() {
// or can I select only pre within $(str) directly?
// everything I tried so far to do that failed miserably
if($(this).is('pre')) {
var code = $('code', $(this)).text();
// brush is my SyntaxHighlighter brush that I created earlier
$('code', $(this)).text(brush.getHtml(code));
// when I console.log($('code', $(this))) now, everything worked
// out perfectly
}
});
// but now I lost all my changes for some reason :-(
return str; // I need to return this as a string again
我是在正確的道路?我如何保持我在.each
循環中所做的更改?
你可能會在這裏找到我的答案有關。 http://stackoverflow.com/questions/1878118/manipulating-the-data-from-ajax-success-functiondata/1878227#1878227 – jpsimons