1

我正在開發一個Firefox附加組件。目前,一旦它收集到了必要的信息,並在後面完成了一些正則表達式的魔法,該插件將所有內容都轉換爲另一個面板中的html格式。下面是格式/代碼分析所收集的正則表達式數據:firefox addon - text result - > xml

self.on("message", function onMessage(storedHistories) { 
var historyList = $('#history-list'); 
historyList.empty(); 
storedHistories.forEach(
function(storedHistory) { 
    var historyHtml = $('#template .history-details').clone();  
    historyHtml.find('.generator-text').text(storedHistory.generatorText); 
    historyHtml.find('.selection-parent-text').html(storedHistory.anchorpText); 
    historyList.append(historyHtml); 
    }); 
}); 

,它結合上述結果看起來像這樣的HTML文件:

<div id="template"> 
    <div class="history-details"> 
    <div class="generator-text"></div> 
    <div class="selection-parent-text"></div>  
    </div> 
</div> 

有沒有辦法把文本從js,並將其輸出爲XML?我需要它來顯示正確的XML容器,並且當它以HTML格式化時,容器就會消失(我假設它們被認爲是HTML標籤)。這可能是一個非常愚蠢的問題,但我真的很喜歡這個,幾乎沒有任何有關XML/js的經驗。我確實在某處讀過一個人可以使用XMLhttpRequest來做到這一點,但我不知道從哪裏開始以及如何去做。

回答

1

假設瀏覽器將XML視爲一個元素並僅顯示標記的內容,您是正確的。要使瀏覽器也顯示標籤,您需要將XML編碼爲HTML(例如,<變爲&lt;)。我能想到的最簡單的方法來做到這一點,因爲你已經在使用jQuery(假設storedHistory.generatorTextstoredHistory.anchorpText是XML字符串),就是改變這兩條線:

historyHtml.find('.generator-text').text(storedHistory.generatorText); 
historyHtml.find('.selection-parent-text').html(storedHistory.anchorpText); 

到:

historyHtml.find('.generator-text').text($('<div />').text(storedHistory.generatorText).html()); 
historyHtml.find('.selection-parent-text').text($('<div />').text(storedHistory.anchorpText).html()); 

有關更多詳細信息,請參見HTML-encoding lost when attribute read from input field

希望這會有所幫助。

+0

'數據'是如何將顯示。你想在可摺疊的TreeView中使用它嗎? – pete 2012-02-02 00:29:38

+0

我嘗試過,但是我得到的解決方案和我一樣,如果我用.text替換.html,那麼得到的結果是相同的:'historyHtml.find('。selection-parent-text')。text(storedHistory.anchorpText); ' 它沒有格式化XML格式的文本。我的字符串看起來是這樣的: ' 數據 ' 或至少上面我希望達到的效果。字符串看起來就像是所有的東西都混在一起了' ...'你明白了。 – spex2004 2012-02-02 00:30:17

+0

抱歉發生了所有這些奇怪的評論事情。 是的,我想在一個可摺疊的視圖。那可能嗎? – spex2004 2012-02-02 00:31:11