DEMO:http://jsbin.com/ohaxa/4/edit
$(function() {
$('#menu li > a').click(function(e) {
e.preventDefault();
var my_id = $(this).attr('id'); // li a #
$.ajax({
type: "POST",
url: "ajax_call.php",
data: "action=do_something&my_id=" + my_id,
success: function(data){
my_function(my_id); //run your outside function...
//OR use "data" from the php echo and append it to DOM
// $(#ajax_call_back).append(data);
},
complete: function(data){
//on complete do something..
},
error: function(data){
//this here for demo pseudo ajax handle error instead!
my_function(my_id);
},
});
});
});
function my_function(id) {
alert(id);
var map = '';
switch(id)
{
case 1:
map = id; //map_1
break;
case 2:
map = id; //map_2
break;
case 3:
map = id; //map_3
break;
default:
map = id; //map_0
}
$('#application').text('map_' + map);
};
<ul id="menu">
<li><a href="#" id="1">menu_1</a></li>
<li><a href="#" id="2">menu_2</a></li>
<li><a href="#" id="3">menu_3</a></li>
<li><a href="#" id="4">menu_4</a></li>
<li><a href="#" id="5">menu_5</a></li>
</ul>
<div id="application"></div>
<div id="ajax_call_back"></div>
當然使用AJAX工作的框架,特別是如果你是新來的JS和AJAX。我在兩個月前開始經常使用jQuery,現在我可以編寫非常複雜的AJAX方法。有了這個平臺可以讓事情變得更容易。有關更多詳細信息,請參閱下面的答案。 – dclowd9901