[英]innerHTML after XMLHttpRequest not working on IE9
I have the following black of code which is working perfectly fine on Chrome & Firefox but fails everytime on IE, it returns "undefined" in the console tab. 我有以下黑色代码,它们在Chrome和Firefox上运行正常,但每次在IE上均失败,它在控制台选项卡中返回“未定义”。
<html>
<head>
<script type="text/javascript" charset="utf-8" src="/js/jquery-latest.js"></script>
<script>
$(document).ready(function()
{
test();
});
function test()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","/xml/products.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var list = xmlDoc.getElementsByTagName("product");
console.log(list[0].childNodes[1].innerHTML);
}
</script>
</head>
</html>
The XML i'm using is the following : 我正在使用的XML如下:
Thanks for your time. 谢谢你的时间。
EDIT: jQuery ajax version not working either: 编辑:jQuery ajax版本不能正常工作:
var xmlDoc;
$.ajax({
type: "GET",
url: "/xml/products.xml",
dataType: "xml",
success: function (xml) {
xmlDoc = xml;
var list=xmlDoc.getElementsByTagName("product");
console.log(list[1].childNodes[1].innerHTML );
}
});
No idea why this works in Chrome and FF, it shouldn't actually 1 . 不知道为什么在Chrome和FF中可以正常工作,实际上不应该1 。
You are loading an XML document, and are successfully selecting an XML element node. 您正在加载XML文档,并成功选择了XML元素节点。 Those don't have innerHTML
properties 2 . 那些没有innerHTML
属性2 。 You should use an XMLSerializer
if you really need to get the markup of your xml document (maybe you're just looking for the .textContent
?). 如果确实需要获取xml文档的标记,则应该使用XMLSerializer
(也许您只是在寻找.textContent
吗?)。
var el = list[0].childNodes[1];
console.log(new XMLSerializer().serializeToString(el));
However, oldIE doesn't even know that, you'll need to use el.xml
for them 3 . 但是,oldIE甚至不知道,您需要为它们使用el.xml
3 。
1: At least not in older versions. 1:至少在旧版本中没有。 See also does innerHTML work with XML Elements? 另请参阅innerHTML是否可与XML Elements一起使用? . 。
2: Apparently, the DOM Parsing spec now includes a generic innerHTML
attribute on all DOM elements 2:显然,DOM分析规范现在在所有DOM元素上都包含通用的innerHTML
属性
3: see JavaScript: Replacement for XMLSerializer.serializeToString()? 3:请参阅JavaScript:是否可以替换XMLSerializer.serializeToString()?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.