jQuery:如何添加XML节点? [英] JQuery: how to add XML node?
问题描述
假设存在parentNode,并且我想向其中添加元素"Child".遵循直观的代码将不起作用:
Assume parentNode exists and I want to add an element "Child" to it. Following intutive code won't work:
$("<Child>").appendTo(parentNode);
因为jQuery将创建一个节点并附加到parentNode.
Because jQuery will create a node and append to parentNode.
所以我想知道,如何在jQuery中添加xml子节点?
So I am wondering, how do you add xml child node in jQuery?
p.s.遵循丑陋的代码会起作用,但确实非常丑陋:
p.s. Following ugly code will work, but it is really really ugly:
parentNode.appendChild(parentNode.ownerDocument.createElement("Child"));
p.s.2 $(parentNode).append('<Child >)
不会在FireFox 3上附加带有jQuery 1.2.6的子节点.如果使用appendTo()
,它将附加名称为CHILD(全大写)的节点.
p.s.2 $(parentNode).append('<Child >)
won't append the child node with jQuery 1.2.6 on FireFox 3. Actually it append nothing. If use appendTo()
, it will append a node with name CHILD (all capital).
推荐答案
JQuery并不意味着处理xml.当您使用$("<Child/>")
时,JQuery使用隐藏的div innerHTML来构建子节点,这就是大写字母不同的原因.
JQuery is not meant to treat xml. When you use $("<Child/>")
JQuery uses a hidden div innerHTML to build the child node, that's why the capitalization differs.
这篇关于jQuery:如何添加XML节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!