2012-06-23 68 views
0

我正在使用jQuery和JavaScript的xml工作。我使用ajax导入xml然后我想操纵它,appendChild在IE8中是一个问题。IE8 appendChild XML问题:类型不匹配

这是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中出现错误弹出窗口

+0

在哪一行这个错误出现? – Coder

+0

我改进了原始问题的细节 – Mike

回答

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

+0

当我尝试导入xml:success:function(data){input_xml = data;}使用$ .parseXML(data)我收到无效的XML:[对象XMLDocument]但xml看起来有效我 – Mike

+0

是的,当你得到XML的时候在ajax中使用它 – charlietfl