2013-09-28 152 views
2

我有XML字符串表示一些HTML標籤。我想選擇一些XML並將其導入到HTML。導入XML到HTML

目前我在做什麼一個例子:

<html> 
<head> 
    <meta charset="utf-8"> 
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 

</head> 
<body> 
<div><p>som text</p></div> 
<h1>hi there</h1> 
<button>click</button> 
<script> 

    xml = '<?xml version="1.0" encoding="utf-8"?><root><html><a href="#">the new link</a></html></root>'; 
    $xml =$($.parseXML(xml)); 

$('button').on('click',function(){ 
    var content = $xml.find('html:first').contents(); 
    content.appendTo('body'); 
}); 
</script> 
</body> 
</html> 

的代碼應該添加在頁面的最後一個鏈接,但我只能看到文字,它不是鏈接。沒有HTML標籤的作品。

我用另一個庫要做到這一點,而不是像jQuery的https://github.com/jindw/xmldom,但結果是一樣的。 更新:這是jsfiddle鏈接:http://jsfiddle.net/V4gHz/

+0

.html()不適用於xml文檔。因爲它裏面使用了innerHTML函數。 –

+0

'.text()'怎麼辦? – Jashwant

+0

nope。在所有 –

回答

1

終於我找到了我自己的問題的答案。這件事不會浪費你的時間與innerHTML或XMLSerializer。因爲他們不適用於所有的瀏覽器,他們只是與Firefox兼容。和jQuery的解決方案,如$ .contents()或$ .HTML()也適用於Firefox,並不適用於歌劇,鉻和IE。

我找到一個名爲「htmlize」 wiche一個jQuery插件,是真正的串行器。插件地址是:https://github.com/ZeeAgency/jquery.htmlize

你可以找到從GitHub的附加信息。這是我的問題如何解決:

<html> 
<head> 
<meta charset="utf-8"> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 

</head> 
<body> 
    <div><p>som text</p></div> 
    <h1>hi there</h1> 
    <button>click</button> 
    <script> 

    xml = '<?xml version="1.0" encoding="utf-8"?><root><html><a href="#">the new link</a></html></root>'; 
    $xml =$($.parseXML(xml)); 

$('button').on('click',function(){ 
    var content = $xml.find('html:first').htmlize(); 
    $(content).appendTo('body'); 
}); 
</script> 
</body> 
</html>