
二级菜单
$("span").click(function(event){
$(this).next().show();
})
$(document).click(function(){
$("ul").hide();
})
})
可是这种方法导致点击span没有效果。因为下面的span包含在document中。这就涉及到事件冒泡,即:点击span的时候,适可而止,不能再向下点击到了document。在点击span的时候,阻止事件冒泡即可:
$("span").click(function(event){
//防止事件冒泡
event = event||window.event;
event.stopPropagation();
$(this).next().show();
})
$(document).click(function(){
$("ul").hide();
})
完整代码如下,您可以运行代码,点击试试:
提示:您可以先修改部分代码再运行
