我正在尝试使用 jQuery 以跨浏览器兼容的方式从 xml 中提取值。我在 Firefox 中执行此操作没有任何问题,但不幸的是这也必须兼容 IE。
我的 jQuery 代码如下所示:
$(document).ready(function()) {
$.get("file.xml", {}, function(parseRefreshTime){
alert('This line is executed in IE.');
$("created", parseRefreshTime).each(function() {
alert('This line is *not* executed in IE.');
refreshTime = $(this).text();
//do stuff with refreshtime
});
});
});
这会提取 <created>
的节点值我的 xml 文件中的节点。
我在我的页面中引用了 jQuery 库,并且它在 Firefox 中正确解析,因此我假设我的解析代码是合适的。我在 Firefox 中收到两个警报,但在 IE 中只收到第一个警报。
我可以发誓我昨天有非常相似的代码,但我一定是调整了一些东西并以某种方式破坏了它。在与它战斗了近一个小时后,我正在寻找另一双眼睛。
有人能发现我在这里做错了什么吗?
最佳答案
一些事情:
- 将 AJAX 请求的响应类型指定为
xml
- 将返回的 XML 对象包装在
$(doc)
中,并使用find
查询 XML - 我认为您的第一行有一些拼写错误:
reader
应该ready
并且您有一个额外的右括号
这对我在 IE6 上有效。如果这对您不起作用,您可能需要检查是否正确提供了 xml。
index.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$.get("test.xml", null, function(doc) {
$(doc).find('created').each(function() {
alert($(this).text());
})
}, 'xml');
});
</script>
</head>
<body>
</body>
</html>
test.xml:
<?xml version="1.0" encoding="UTF-8"?>
<created>2010-01-07 00:00:00</created>
关于xml - 在 IE6 中使用 jQuery 解析 XML 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1947331/