帮我看看这段jquery代码有什么问题,为什么点击子菜单时,主菜单会闭合
<!doctypehtml><html><head><metacharset="utf-8"><title>无标题文档</title></head><scriptsrc=...
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" type="text/javascript"></script>
<style>
.submenu{
display:none;
}
</style>
<script>
$(function() {
var submenu=$(".submenu");
var submenu_parent=submenu.parent();
$(submenu_parent).each(function(i){
$(submenu_parent[i]).click(function(){
if($(submenu_parent[i]).children("ul").css("display")=="none"){
$(submenu_parent[i]).children("ul").slideDown(500);
}
else{
$(submenu_parent[i]).children("ul").slideUp(500)
.parents("ul").show();
}
});
});
});
</script>
<body>
<ul>
<li><a href="#" class="item">菜单一1</a>
<ul class="submenu">
<li><a href="#">菜单二1</a>
<ul class="submenu">
<li><a href="#">菜单三</a>
<ul class="submenu">
<li><a href="#">菜单四</a></li>
<li><a href="#">菜单四</a></li>
<li><a href="#">菜单四</a></li>
<li><a href="#">菜单四</a></li>
<li><a href="#">菜单四</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</body>
</html> 展开
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" type="text/javascript"></script>
<style>
.submenu{
display:none;
}
</style>
<script>
$(function() {
var submenu=$(".submenu");
var submenu_parent=submenu.parent();
$(submenu_parent).each(function(i){
$(submenu_parent[i]).click(function(){
if($(submenu_parent[i]).children("ul").css("display")=="none"){
$(submenu_parent[i]).children("ul").slideDown(500);
}
else{
$(submenu_parent[i]).children("ul").slideUp(500)
.parents("ul").show();
}
});
});
});
</script>
<body>
<ul>
<li><a href="#" class="item">菜单一1</a>
<ul class="submenu">
<li><a href="#">菜单二1</a>
<ul class="submenu">
<li><a href="#">菜单三</a>
<ul class="submenu">
<li><a href="#">菜单四</a></li>
<li><a href="#">菜单四</a></li>
<li><a href="#">菜单四</a></li>
<li><a href="#">菜单四</a></li>
<li><a href="#">菜单四</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</body>
</html> 展开
1个回答
推荐于2016-02-09
展开全部
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="keywords" content="白菜编辑部">
<title>白菜编辑部</title>
<style type="text/css">
.submenu {
display: none;
}
</style>
<script type="text/javascript" src="jquery-1.8.0.min.js"></script>
<script type="text/javascript">
$ (function ()
{
$ ('ul>li>a').click (function ()
{
$ (this).next ('ul').slideToggle (500);
});
});
</script>
</head>
<body>
<ul>
<li><a href="#" class="item">菜单一1</a>
<ul class="submenu">
<li><a href="#">菜单二1</a>
<ul class="submenu">
<li><a href="#">菜单三</a>
<ul class="submenu">
<li><a href="#">菜单四</a></li>
<li><a href="#">菜单四</a></li>
<li><a href="#">菜单四</a></li>
<li><a href="#">菜单四</a></li>
<li><a href="#">菜单四</a></li>
</ul>
</li>
</ul>
</li>
</ul></li>
</ul>
</body>
</html>
更多追问追答
追问
非常感谢!我想知道,我的问题出在哪呢?
追答
你的问题就是简单的问题搞复杂了,在实际工作当中这个是非常不可取的也是效率极低的,而且结果不对的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询