我可以一次性将Ajax requestXML对象附加到我的文档树吗? [英] Can I append an Ajax requestXML object to my document tree all in one go?

查看:105
本文介绍了我可以一次性将Ajax requestXML对象附加到我的文档树吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候。
这是我的服务器在responseXML对象中返回的XML对象:

Greetings. Here is an XML object returned by my server in the responseXML object:

<tableRoot>
   <table>
      <caption>howdy!</caption>
      <tr>
         <td>hello</td>
         <td>world</td>
      </tr>
      <tr>
         <td>another</td>
         <td>line</td>
      </tr>
   </table>

现在我将此片段附加到我的文档树如此:

Now I attach this fragment to my document tree like so:

getElementById('entryPoint')。appendChild(responseXML.firstChild.firstChild);

getElementById('entryPoint').appendChild(responseXML.firstChild.firstChild);

但是,不是被渲染为表,我得到以下文本

But instead of being rendered as a table, I get the following text:

多好! helloworldanotherline

howdy! helloworldanotherline

同样的结果发生在我用firstChild替换firstChild.firstChild。
看起来我只是得到了nodeValues,所有的标签都被剥离了?
我从根本上误解了responseXML对象应该代表什么?
这个工作,BTW,如果我拿出根标签,并将innerHTML设置为responseText。

有人可以以正确的方式启发我使用responseXML吗?

The same result occurs of I replace firstChild.firstChild with just firstChild. It seems like I'm just getting the nodeValues, and all of the tags are stripped out?! Am I fundamentally misunderstanding what the responseXML object is supposed to represent? This works, BTW, if I take out the 'root' tags, and set innerHTML to responseText.
Can someone please enlighten me on the correct way to use responseXML?

推荐答案

您获取文本而不是表,因为您使用纯DOM进行操作,并且您的响应XML没有命名空间声明。所以当附加一个XML元素浏览器不知道你的表标签是从HTML,XUL,SVG还是从。

You get the text instead of a table, because you use pure DOM for manipulations and your response XML doesn't have the namespaces declarations. So when appending an XML element browser doesn't know whether your "table" tag is from HTML, XUL, SVG or else from.

1)添加命名空间声明: / p>

1) Add namespace declaration:

<table xmlns="http://www.w3.org/1999/xhtml">

2)而不是直接插入已提交的XML DOM元素,您应该首先将该节点导入到HTML中DOM文档:

2) Instead of directly inserting a reffered XML DOM Element, you should first import that node into your HTML DOM Document:

var element = document.importNode(responseXML.firstChild.firstChild, true);
document.getElementById('entryPoint').appendChild(element);

希望这有帮助!

这篇关于我可以一次性将Ajax requestXML对象附加到我的文档树吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆