我想使用jQuery读取XML文件。我尝试的示例适用于Chrome,但不适用于Firefox。请帮我编写代码,这样就可以在Firefox中使用了。
HTML code:
<html>
<title> xml try</title>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
var markers = "";
$(document).ready(function () {
$.get("File.xml", {}, function (xml){
$('marker',xml).each(function(i){
markers = $(this).text();
$('#data').html(''+markers);
document.write("hi "+markers);
document.write("");
//alert('it is '+$(this));
i++;
});
});
});
</script>
</head>
<body>
<div id="data"> div is </div>
Hello
</body>
</html>
XML代码:
<markers>
<marker>
<type>1</type>
<title>1</title>
<address>1</address>
<latitude>1</latitude>
<longitude>1</longitude>
<marker>
<marker>
<type>2</type>
<title>2</title>
<address>2</address>
<latitude>2</latitude>
<longitude>2</longitude>
<marker>
</markers>
答案 0 :(得分:2)
只需替换你的xml(你的xml格式错误):
<markers>
<marker>
<type>1</type>
<title>1</title>
<address>1</address>
<latitude>1</latitude>
<longitude>1</longitude>
</marker>
<marker>
<type>2</type>
<title>2</title>
<address>2</address>
<latitude>2</latitude>
<longitude>2</longitude>
</marker>
</markers>
你需要关闭
<marker> </marker>
然后它会正常工作