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