jQuery 1.6.1在XML中添加新节点 [英] jQuery 1.6.1 add a new node in XML
问题描述
jsFiddle URL: http://jsfiddle.net/rcrathore/Y8DpM/
jsFiddle URL : http://jsfiddle.net/rcrathore/Y8DpM/
我正在尝试使用jQuery在XML中添加一个新节点.以下代码在Firefox,Safari和Chrome中运行良好,但在IE8中却出现了错误:
I am trying to add a new node in XML using jQuery. Following code works fine in Firefox, Safari and Chrome but giving error in IE8:
<div id="result"> </div>
<div id="result2"> </div>
<script type="text/javascript">
<!--
var xml = "<root><node1><node2>TEXT1</node2></node1></root>" ;
var $xml = $($.parseXML(xml)) ;
var newData = "<node3>234</node3>" ;
var $newData = $($.parseXML(newData));
var newNode = null;
if (typeof document.importNode == 'function') {
newNode = document.importNode($newData.find('node3').get(0),true);
} else {
newNode = $newData.find('node3').get(0)
}
try {
$(newNode).appendTo($xml.find("node2"));
/* expected TEXT1234*/
$("div#result").html($xml.text());
/* expected 234*/
$("div#result2").html($xml.find("root > node1 > node2 > node3").text());
} catch (e) {
alert(e) ;
$("div#result").html("Error: " + e.description);
}
//-->
</script>
IE8上的错误描述是参数数量错误或属性分配无效".
The error description on IE8 is "Wrong number of arguments or invalid property assignment".
有没有办法在IE8上解决此问题?
Is there a way to fix this on IE8?
推荐答案
如果我替换appendTo行,则该代码在所有浏览器中均有效
The code works in all browsers if I replace appendTo line
$(newNode).appendTo($xml.find("node2"));
作者:
$xml.find("node2").get(0).appendChild(newNode);
修改
根据bugs.jquery.com(9602),此问题将在1.6.2中修复.
As per bugs.jquery.com (9602), this issue will be fixed in 1.6.2.
这篇关于jQuery 1.6.1在XML中添加新节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!