用jQuery编写XML文件 [英] writing XML files with jQuery

查看:73
本文介绍了用jQuery编写XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过jquery更改xml属性很容易,只是:

chaning xml attributes through jquery is easy-peasy, just:

$(this).attr('name', 'hello');

但是如何将另一个标签添加到文件中?我尝试以静默方式使用附加JS模具.

but how can I add another tag into the file? I tried using append the JS dies silently.

有什么办法吗?

说明:此代码是Firefox扩展的一部分,因此不必担心保存到用户文件系统中.对于XML文档,append仍然不起作用,但是我可以更改xml属性值

Clarifications: this code is part of an extension to firefox, so don't worry about saving into the user file system. Still append doesn't work for xml documents yet I can change xml attribute values

推荐答案

问题是jQuery正在网页的当前文档中创建新节点,因此结果该节点可以不能附加到其他XML文档.因此,必须在XML文档中创建该节点.

The problem is that jQuery is creating the new node in the current document of the web page, so in result the node can't be appended to a different XML Document. So the node must be created in the XML Document.

您可以这样做

var xml = $('<?xml version="1.0"?><foo><bar></bar><bar></bar></foo>'); // Your xml
var xmlCont = $('<xml>'); // You create a XML container
xmlCont.append(xml); // You append your XML to the Container created in the main document

// Now you can append without problems to you xml
xmlCont.find('foo bar:first').append('<div />');

xmlCont.find('foo bar div'); // Test so you can see it works

这篇关于用jQuery编写XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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