开发者

jQuery 1.6.1 add a new node in XML

jsFiddle URL : http://jsfiddle.net/rcrathore/Y8DpM/

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);
    } e开发者_运维知识库lse {
         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>

The error description on IE8 is "Wrong number of arguments or invalid property assignment".

Is there a way to fix this on IE8?


The code works in all browsers if I replace appendTo line

$(newNode).appendTo($xml.find("node2"));

by :

$xml.find("node2").get(0).appendChild(newNode);

Edit

As per bugs.jquery.com (9602), this issue will be fixed in 1.6.2.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜