data.xml中
<?xml version="1.0" encoding="utf-8"?>
<cars>
<car company="TOYOTA">
<product>Prius</product>
</car>
<car company="TOYOTA">
<product>New Fortuner</product>
</car>
</cars>
的index.html
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
$('#car').click(function() {
$.get('data.xml',function(data){
$('#content').empty();
$(data).find('car').each(function(){
var $car = $(this);
var html = '<div class="data">';
html += '<h3>' + $car.attr('company') + '</h3>';
$('#content').append(html);
});
});
return false;
});
});
</script>
</head>
<body>
<a href="#" id="car">Car</a>
<div id="content">
</div>
</body>
</html>
上面的代码是从这里取:http://www.phpeveryday.com/articles/jQuery-AJAX-and-XML-P970.html
问:
在index.html中,$(data).find('car').each
,这是如何工作的:$(data)
?通常我会看到$('selector')
,如$('.class')
,$('#id')
,但数据是xml文件。任何人都可以向我解释这一点?谢谢。
在成功回调中,'data'不是XML *文件*,它是XML *文档根节点*。想想'$(document.body)'是如何工作的 - 它基本上是一样的。 – DCoder
或者'data'是一个XML字符串,它和$(“