1
我在尝试修改success
回调中的全局变量时遇到了jQuery问题:jQuery AJAX范围的问题
<html>
<head>
<script type="text/javascript" src="javascript/jquery.min.js"></script>
<script type="text/javascript">
// Define items in the global scope
items = null;
// Get items from XML for a given category ID
function getItems(categoryID)
{
$.ajax({
type: 'GET',
url: 'items.xml',
dataType: 'xml',
success: function(xml){
items = $(xml).find('category[id="'+categoryID+'"]').children().first();
// This works (returns the name of the first item)
alert(items.attr('name'));
}
});
}
</script>
</head>
<body>
<script type="text/javascript">
$(function(){
getItems(1);
// This doesn't work (returns null)
alert(items.attr('name'));
});
</script>
</body>
</html>
我做错了什么?
完美答案,谢谢! – gjb 2011-02-23 00:19:50