添加XML DOM元素与jQuery-这可能吗? [英] Adding XML DOM elements with jQuery- Is it possible?

查看:173
本文介绍了添加XML DOM元素与jQuery-这可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用jQuery来操纵我已经加载使用Ajax的XML文件的DOM。我可以选择元素,空他们,并删除它们,而不是将它们添加。

jQuery的很少让我失望,但我感觉烧毁和击败。请帮忙。

在code:

 <脚本类型=文/ JavaScript的>

$阿贾克斯({
    键入:GET,
    网址:note.xml的,
    数据类型:XML,
    成功:parseResultsXML
});

功能parseResultsXML(XML){

    $(XM​​L).append('<项目>东西< /项目>'); //没有被添加! :(
}

< / SCRIPT>
 

解决方案

我想你会需要使用$ .parseXML(XML)来解析XML。 parseXML()是新增加的jQuery 1.5。我有类似的问题,因为你,我结束了使用一些插件库,因为我使用jQuery 1.4来解析XML。

http://api.jquery.com/jQuery.parseXML/

====编辑====

这是一个有点棘手使用jQuery来处理XML。试试下面的code

  VAR DOM = $ .parseXML(XML); //返回DOM元素
VAR项目= $ .parseXML(<项目>东西< /项目>中); //返回DOM元素

$(DOM)。儿童(0).append($(项目)。儿童(0))

的console.log($(DOM).find(项目)文本()); //应打印东西
 

您需要做的$(DOM)。儿童(...),因为DOM是文档对象,你就必须要用到的jQuery为了使用jQuery功能。

您需要做的 $(DOM)。儿童(0).append(...)而不是 $(DOM).append ()因为DOM指的是XML DOM树的文档根目录。召回所有DOM是这样的:

 文档
     |
    根
  / | \
 C1 C2 C3 ....更多的孩子......
 

所以,如果你这样做 $(DOM).append(..)你实际上是试图追加到这是不允许的,因为文档根目录下只能有文档根目录一个元素的孩子,你的XML文档,即根元素。

同样,你做的 .append($(项目)。儿童(0)),因为要插入项目的根元素<中/ code>不是文档根目录项目

I am trying to use jQuery to manipulate the DOM of an XML file I've loaded with ajax. I can select elements, empty them, and remove them, but not add them.

jQuery rarely lets me down, but I am feeling burnt out and defeated. Please help.

The code:

<script type="text/javascript">

$.ajax({
    type: "GET",
    url: "note.xml",
    dataType: "xml",
    success: parseResultsXML
});

function parseResultsXML(xml) {

    $(xml).append('<item>something</item>'); // Nothing gets added! :(
}

</script>

解决方案

I think you will need to use $.parseXML(xml) to parse the XML. parseXML() was new addition to jQuery 1.5. I had similar problem as you, and I ended up using some plugin library to parse XML because I was using jQuery 1.4.

http://api.jquery.com/jQuery.parseXML/

==== Edit ====

It's a bit tricky to manipulate XML using jQuery. Try the following code

var dom = $.parseXML(xml); //returns DOM element
var item = $.parseXML("<item>something</item>"); //returns DOM element

$(dom).children(0).append($(item).children(0))

console.log($(dom).find("item").text()); //should print "something"

You have to do $(dom).children(...) because dom is a document object, you have to wrap it into jQuery in order to use jQuery functions.

You have to do $(dom).children(0).append(...) but not $(dom).append() because dom refers to the document root of the XML DOM tree. recall all DOM looks like:

  Document
     |
    root
  /  |  \   
 c1  c2  c3 ....more children...

So if you do $(dom).append(..) you are actually trying to append to the document root which is not allowed because document root can only have one element child, namely the root element of your xml document.

Similarly, you do .append($(item).children(0)) because you want to insert the root element of item not the document root of item

这篇关于添加XML DOM元素与jQuery-这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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