我對HTMLjQuery的通話功能到功能
<ul id="menu">
<li class="mainmenu" id="newsarticles"><a href="#">News & articles</a>
<ul id="newsarticles">
<li class="submenu" id="news"> <a href="#">News</a>
</li>
<li class="submenu" id="articles"> <a href="#">Articles</a>
</li>
</ul>
<li class="mainmenu" id="contactus"><a href="#">Contact Us</a>
</li>
</ul>
這個代碼,我調用使用jQuery,以創建一個新的樹型菜單中調用HTML頁面阿賈克斯這段代碼,我的問題是,我想使用這兩個菜單相同的功能,而且我不知道是否有辦法做到這一點,而無需編寫相同功能的兩倍:
$(document).ready(function() {
$("#menu li").on("click", function(e) {
e.stopPropagation();
var currentId = $(this).attr('id');
var scriptPath = currentId+".html";
var tree = $(this).closest("ul").attr("id");
$.ajax({
url: scriptPath,
type:'HEAD',
success: function()
{
//file exists
$("#tree").html($("#"+tree).html());
$("#text").load(scriptPath);
$("#tree li").click(Menu_callTabs); //the same function for tree menu
}
});
});
});
function Menu_callTabs(){
$("#menu li").on("click", function(e) {
var currentId = $(this).attr('id');
var scriptPath = currentId+".html";
var tree = $(this).closest("ul").attr("id");
$.ajax({
url: scriptPath,
type:'HEAD',
success: function()
{
//file exists
$("#tree").html($("#"+tree).html());
$("#text").load(scriptPath);
$("#tree li").click(Menu_callTabs); //the same function for tree menu
}
});
});
}
任何幫助表示讚賞。
使用回調函數。 – 2013-03-03 14:53:11