我对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