简体   繁体   English

IE 9 ::使用Javascript将XML代码显示到Div中

[英]IE 9 :: Display XML code into a Div with Javascript

I've been trying to display my XML code into a Div with Javascript but in the Internet Explorer won't work as follows: 我一直试图将XML代码显示为带有Java脚本的Div,但在Internet Explorer中将无法按以下方式工作:

$('footer #xml_content').val($parsed.find('poem').html());
$('div#explanation').html($parsed.find('explanation').html());

Although it does works with the functions below as text() but the XML tags won't display: 尽管它可以与下面的text()函数一起使用,但是XML标签不会显示:

$('footer #xml_content').html($parsed.find('poem').text());
$('div#explanation').html($parsed.find('explanation').text());

Can anyone can figure out this puzzle? 谁能解决这个难题? Thank you! 谢谢!

function go_poem(pnum, poem, live, id_timer1, id_timer2, reset) { 
    $.ajax({ 
        type: "GET", 
        url: _list_poems[ parseInt(pnum)-1 ], 
        dataType: "xml", 
        success: function (xml) { 
            var $parsed = $(xml); 
            var $texto = $parsed.find("texto"); 
            $('footer #xml_content').html(
                document.createTextNode( $('poem').html() )
            );
            $('div#explanation').html(
                document.createTextNode( $('explanation').html() )
            );
/* Remainder of code not included */

You have to escape all <> with &lt; 您必须使用&lt;转义所有<> &lt; or &gt; &gt; if you actually want to display those tags as text. 如果您实际上想将这些标签显示为文本。

Updated Answer 更新的答案

After speaking with the OP in a chat, we settled on the following pattern: 在与OP聊天后,我们确定了以下模式:

var box = $("<div></div>");

$("footer #xml_content").html(function () {
    box.html( $parsed.find("poema").clone() );
    return box.html();
});

Due to permission restrictions we had to add the XML node to a temporary node and then return the html of the temporary, unattached, node. 由于权限限制,我们必须将XML节点添加到临时节点,然后返回临时未连接节点的html。

Original Answer 原始答案

Create a textNode from the XML: 从XML创建一个textNode

document.createTextNode( $parsed.find('poem').html() );

Note however that your markup may not be exactly what you wrote to begin with. 但是请注意,您的标记可能与您最初写的完全不同。 I've created a small fiddle to demonstrate this approach, as well as included some invalid markup at the end for demonstration purposes. 我创建了一个小提琴来演示这种方法,并在末尾添加了一些无效的标记以进行演示。

The browser is will attempt to repair/re-arrange elements and attributes. 浏览器将尝试修复/重新排列元素和属性。 As a result, what you retrieve from $.fn.html may not reflect what was given to the parser to begin with. 结果,您从$.fn.html检索的$.fn.html可能无法反映开始时提供给解析器的内容。

http://jsfiddle.net/jonathansampson/zvxj3yh4/ http://jsfiddle.net/jonathansampson/zvxj3yh4/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM