最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - writing XML files with jQuery - Stack Overflow

programmeradmin5浏览0评论

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

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

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

Is there any way to do this?

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

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

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

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

Is there any way to do this?

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

Share Improve this question edited Jul 6, 2009 at 13:32 CamelCamelCamel asked Jul 6, 2009 at 12:17 CamelCamelCamelCamelCamelCamel 5,2008 gold badges63 silver badges94 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 10

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.

You can do this like so

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

I'd suggest you walk through the code with a debugger and see if you can determine why the append is causing an error (or if the error is someplace else). Something like:

$('selector').append('<p></p>');

should work just fine.

发布评论

评论列表(0)

  1. 暂无评论