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文件。谁有人向我解释这个?感谢。
答案 0 :(得分:0)
$.get('data.xml',function(data)
在你调用了数据中的整个xml内容之后。所以'数据'包含汽车标签。
然后
$(data).find('car').each(function(){ ... })
查找数据中的每个'car'标记,即xml内容
仅供参考:为了更好地理解使用FIREBUG等调试工具,并对您的javascript代码进行逐步分析