IE8 appendChild到XML问题:输入Mismatch

时间:2012-06-23 17:50:22

标签: javascript jquery xml appendchild

我正在使用jquery和javascript处理xml。我使用ajax导入xml然后我想操纵它,在IE8中appendChild是一个问题。

这是Javascript:

// How i get xml 
$.ajax({
  url: production_get,
  dataType: "xml",
  success: function(data) {
      input_xml=data;
  }
});

// how i try to append a new node to 

new_user_node = document.createElement('user');
new_user_node.setAttribute('id',new_user_id);
new_user_node.setAttribute('label',new_user_label);        

response=$(input_xml)[0].getElementsByTagName("response")[0];
response.appendChild(new_user_node); // <- type mismatch

XML标记

<response>
    <user id="123" label="John" />
</response>

这适用于所有浏览器,但IE报告:类型不匹配。我不得不说它甚至可以在IE8中运行,但是控制台会报告错误,而在IE7中会出现错误弹出

1 个答案:

答案 0 :(得分:1)

当你在jQuery中包装xml时,它会将xml视为html。这允许遍历获取属性和文本,但不足以修改xml。

要创建要附加到您的XML文档,您需要使用$.parseXML()

/* First create xml doc*/
var xmlDoc=$.parseXML(input_xml);

/*Create jQuery object of xml doc*/
var $xml= $( xmlDoc);

/*Now append*/
$xml.append( new_user_node);

http://api.jquery.com/jQuery.parseXML/

API中的更多示例