I have created a menu which is working fine. But on the page load all the menu items are collapsed. A look look on my code
我创建了一个工作正常的菜单。但是在页面加载时,所有菜单项都会折叠。看看我的代码
$(document).ready(function() {
$("#nav > li > a").on("click", function(e) {
if ($(this).parent().has("ul")) {
e.preventDefault();
}
if (!$(this).hasClass("open")) {
// hide any open menus and remove all other classes
$("#nav li ul").slideUp(350);
$("#nav li a").removeClass("open");
// open our new menu and add the open class
$(this).next("ul").slideDown(350);
$(this).addClass("open");
} else if ($(this).hasClass("open")) {
$(this).removeClass("open");
$(this).next("ul").slideUp(350);
}
});
});
/* nav menu styles */
#nav {
display: block;
width: 280px;
margin-left: -40px;
list-style-type: none;
}
#nav li {} #nav > li > a {
display: block;
padding: 8px 7px;
font-size: 1.3em;
font-weight: 200;
color: #889599;
text-decoration: none;
border-bottom: 1px solid #EFEFEF;
background-color: #F7F9FA;
/*background: -webkit-gradient(linear, left top, left bottom, from(#343434), to(#292929));
background: -webkit-linear-gradient(top, #343434, #292929);
background: -moz-linear-gradient(top, #343434, #292929);
background: -ms-linear-gradient(top, #343434, #292929);
background: -o-linear-gradient(top, #343434, #292929);
background: linear-gradient(top, #343434, #292929);*/
}
#nav > li > a:hover,
#nav > li > a.open {
color: #889599;
background-color: #F7F9FA;
}
#nav li ul {
display: none;
background: #EFEFEF;
list-style-type: none;
margin-left: -40px;
}
#nav li ul li a {
display: block;
background: none;
padding: 10px 0px;
padding-left: 30px;
font-size: 1.1em;
text-decoration: none;
font-weight: 200;
color: #889599;
}
#nav li ul li a:hover {
background: #F7F9FA;
}
<link href="http://fortawesome.github.io/Font-Awesome/assets/font-awesome/css/font-awesome.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
<ul id="nav">
<li><a href="#"><i class="fa fa-arrow-circle-o-right"></i> Animation</a>
<ul>
<li><a href="http:/www.google.com/search?q=design+cartoons+animation">Cartoons</a>
</li>
<li><a href="http:/www.google.com/search?q=design+comic+strips+inspiration">Comic Strips</a>
</li>
<li><a href="http:/www.google.com/search?q=how+to+clip+video+footage">Video Clips</a>
</li>
<li><a href="http:/www.google.com/search?q=design+create+animated+gifs">Web GIFs</a>
</li>
</ul>
</li>
<li><a href="#"><i class="fa fa-arrow-circle-o-right"></i> Graphic Design</a>
<ul>
<li><a href="http:/www.google.com/search?q=photoshop+tutorials+graphics+design">Adobe Photoshop</a>
</li>
<li><a href="http:/www.google.com/search?q=digital+branding+graphics+logos">Branding & Logos</a>
</li>
<li><a href="http:/www.google.com/search?q=graphics+design+marketing">Digital Marketing</a>
</li>
<li><a href="http:/www.google.com/search?q=graphic+design+illustrations">Illustrations</a>
</li>
<li><a href="http:/www.google.com/search?q=infographics+inspiration">Infographics</a>
</li>
<li><a href="http:/www.google.com/search?q=product+design+inspiration">Product Design</a>
</li>
</ul>
</li>
<li><a href="#"><i class="fa fa-arrow-circle-o-right"></i> Digital Photography</a>
<ul>
<li><a href="http:/www.google.com/search?q=cityscape+photography">Cityscapes</a>
</li>
<li><a href="http:/www.google.com/search?q=water+ocean+photography">Oceans</a>
</li>
<li><a href="http:/www.google.com/search?q=wide+angle+lens+photography">Wide-Angle Lens</a>
</li>
<li><a href="http:/www.google.com/search?q=wildlife+photography">Wildlife</a>
</li>
</ul>
</li>
<li><a href="#"><i class="fa fa-arrow-circle-o-right"></i> Print & Identity</a>
<ul>
<li><a href="http:/www.google.com/search?q=branding+identity+print+design">Branding</a>
</li>
<li><a href="http:/www.google.com/search?q=business+cards+design">Business Cards</a>
</li>
<li><a href="http:/www.google.com/search?q=print+design+letterpress">Letterpress</a>
</li>
<li><a href="http:/www.google.com/search?q=print+poster+artwork">Poster Art</a>
</li>
</ul>
</li>
<li><a href="#"><i class="fa fa-arrow-circle-o-right"></i> Programming</a>
<ul>
<li><a href="http:/www.google.com/search?q=learn+css3+web+development">CSS3</a>
</li>
<li><a href="http:/www.google.com/search?q=learn+html5+web+development">HTML5</a>
</li>
<li><a href="http:/www.google.com/search?q=javascript+jquery+tutorials">JavaScript & jQuery</a>
</li>
<li><a href="http:/www.google.com/search?q=web+development+mysql">MySQL Databases</a>
</li>
<li><a href="http:/www.google.com/search?q=wordpress+programming">Wordpress CMS</a>
</li>
</ul>
</li>
<li><a href="#"><i class="fa fa-arrow-circle-o-right"></i> Web Design</a>
<ul>
<li><a href="http:/www.google.com/search?q=web+design+icons">Icons</a>
</li>
<li><a href="http:/www.google.com/search?q=web+design+tutorials">Tutorials</a>
</li>
<li><a href="http:/www.google.com/search?q=web+design+user+interface">User Interfaces</a>
</li>
<li><a href="http:/www.google.com/search?q=web+design">Website Layouts</a>
</li>
</ul>
</li>
</ul>
</nav>
On page load all the menu items are collapsed.I only want to expand first menu option on page load.
在页面加载时,所有菜单项都已折叠。我只想在页面加载时展开第一个菜单选项。
Please help me to create this.
请帮我创建这个。
3 个解决方案
#1
2
Just do this:
这样做:
$(document).ready(function(){
$("#nav li:first").find('a').trigger('click');
})
UPDATE
UPDATE
DEMO
$(document).ready(function() {
$("#nav > li > a").on("click", function(e) {
if ($(this).parent().has("ul")) {
e.preventDefault();
}
if (!$(this).hasClass("open")) {
// hide any open menus and remove all other classes
$("#nav li ul").slideUp(350);
$("#nav li a").removeClass("open");
// open our new menu and add the open class
$(this).next("ul").slideDown(350);
$(this).addClass("open");
} else if ($(this).hasClass("open")) {
$(this).removeClass("open");
$(this).next("ul").slideUp(350);
}
});
$("#nav > li:first > a").trigger('click');
//Or else you can go with below one too
//$("#nav li:first").find('a').trigger('click');
});
#2
2
Just trigger click on the first item. You can use :first
Pseudo Class
只需触发点击第一项即可。您可以使用:first Pseudo Class
$(document).ready(function() {
$("#nav > li > a").on("click", function(e) {
if ($(this).parent().has("ul")) {
e.preventDefault();
}
if (!$(this).hasClass("open")) {
// hide any open menus and remove all other classes
$("#nav li ul").slideUp(350);
$("#nav li a").removeClass("open");
// open our new menu and add the open class
$(this).next("ul").slideDown(350);
$(this).addClass("open");
} else if ($(this).hasClass("open")) {
$(this).removeClass("open");
$(this).next("ul").slideUp(350);
}
});
$("#nav > li:first > a").trigger('click');
});
/* nav menu styles */
#nav {
display: block;
width: 280px;
margin-left: -40px;
list-style-type: none;
}
#nav li {} #nav > li > a {
display: block;
padding: 8px 7px;
font-size: 1.3em;
font-weight: 200;
color: #889599;
text-decoration: none;
border-bottom: 1px solid #EFEFEF;
background-color: #F7F9FA;
/*background: -webkit-gradient(linear, left top, left bottom, from(#343434), to(#292929));
background: -webkit-linear-gradient(top, #343434, #292929);
background: -moz-linear-gradient(top, #343434, #292929);
background: -ms-linear-gradient(top, #343434, #292929);
background: -o-linear-gradient(top, #343434, #292929);
background: linear-gradient(top, #343434, #292929);*/
}
#nav > li > a:hover,
#nav > li > a.open {
color: #889599;
background-color: #F7F9FA;
}
#nav li ul {
display: none;
background: #EFEFEF;
list-style-type: none;
margin-left: -40px;
}
#nav li ul li a {
display: block;
background: none;
padding: 10px 0px;
padding-left: 30px;
font-size: 1.1em;
text-decoration: none;
font-weight: 200;
color: #889599;
}
#nav li ul li a:hover {
background: #F7F9FA;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
<ul id="nav">
<li><a href="#">Animation</a>
<ul>
<li><a href="http:/www.google.com/search?q=design+cartoons+animation">Cartoons</a>
</li>
<li><a href="http:/www.google.com/search?q=design+comic+strips+inspiration">Comic Strips</a>
</li>
<li><a href="http:/www.google.com/search?q=how+to+clip+video+footage">Video Clips</a>
</li>
<li><a href="http:/www.google.com/search?q=design+create+animated+gifs">Web GIFs</a>
</li>
</ul>
</li>
<li><a href="#">Graphic Design</a>
<ul>
<li><a href="http:/www.google.com/search?q=photoshop+tutorials+graphics+design">Adobe Photoshop</a>
</li>
<li><a href="http:/www.google.com/search?q=digital+branding+graphics+logos">Branding & Logos</a>
</li>
<li><a href="http:/www.google.com/search?q=graphics+design+marketing">Digital Marketing</a>
</li>
<li><a href="http:/www.google.com/search?q=graphic+design+illustrations">Illustrations</a>
</li>
<li><a href="http:/www.google.com/search?q=infographics+inspiration">Infographics</a>
</li>
<li><a href="http:/www.google.com/search?q=product+design+inspiration">Product Design</a>
</li>
</ul>
</li>
<li><a href="#">Digital Photography</a>
<ul>
<li><a href="http:/www.google.com/search?q=cityscape+photography">Cityscapes</a>
</li>
<li><a href="http:/www.google.com/search?q=water+ocean+photography">Oceans</a>
</li>
<li><a href="http:/www.google.com/search?q=wide+angle+lens+photography">Wide-Angle Lens</a>
</li>
<li><a href="http:/www.google.com/search?q=wildlife+photography">Wildlife</a>
</li>
</ul>
</li>
<li><a href="#">Print & Identity</a>
<ul>
<li><a href="http:/www.google.com/search?q=branding+identity+print+design">Branding</a>
</li>
<li><a href="http:/www.google.com/search?q=business+cards+design">Business Cards</a>
</li>
<li><a href="http:/www.google.com/search?q=print+design+letterpress">Letterpress</a>
</li>
<li><a href="http:/www.google.com/search?q=print+poster+artwork">Poster Art</a>
</li>
</ul>
</li>
<li><a href="#">Programming</a>
<ul>
<li><a href="http:/www.google.com/search?q=learn+css3+web+development">CSS3</a>
</li>
<li><a href="http:/www.google.com/search?q=learn+html5+web+development">HTML5</a>
</li>
<li><a href="http:/www.google.com/search?q=javascript+jquery+tutorials">JavaScript & jQuery</a>
</li>
<li><a href="http:/www.google.com/search?q=web+development+mysql">MySQL Databases</a>
</li>
<li><a href="http:/www.google.com/search?q=wordpress+programming">Wordpress CMS</a>
</li>
</ul>
</li>
<li><a href="#">Web Design</a>
<ul>
<li><a href="http:/www.google.com/search?q=web+design+icons">Icons</a>
</li>
<li><a href="http:/www.google.com/search?q=web+design+tutorials">Tutorials</a>
</li>
<li><a href="http:/www.google.com/search?q=web+design+user+interface">User Interfaces</a>
</li>
<li><a href="http:/www.google.com/search?q=web+design">Website Layouts</a>
</li>
</ul>
</li>
</ul>
</nav>
#3
0
One solution is that you can hard code in html to open that menu
一种解决方案是您可以在html中硬编码以打开该菜单
add open class to a
and style='display:block'
to your ul
将open类添加到a并将style ='display:block'添加到ul
<ul id="nav">
<li><a class="open" href="#">Animation</a>
<ul style="display: block;">
<li><a href="http:/www.google.com/search?q=design+cartoons+animation">Cartoons</a>
</li>
<li><a href="http:/www.google.com/search?q=design+comic+strips+inspiration">Comic Strips</a>
</li>
Or
You can add this before your click event
您可以在点击事件之前添加此项
$("nav").find("li:first a").addClass("open");
$("nav").find("li:first ul").show();
here is a Example
#1
2
Just do this:
这样做:
$(document).ready(function(){
$("#nav li:first").find('a').trigger('click');
})
UPDATE
UPDATE
DEMO
$(document).ready(function() {
$("#nav > li > a").on("click", function(e) {
if ($(this).parent().has("ul")) {
e.preventDefault();
}
if (!$(this).hasClass("open")) {
// hide any open menus and remove all other classes
$("#nav li ul").slideUp(350);
$("#nav li a").removeClass("open");
// open our new menu and add the open class
$(this).next("ul").slideDown(350);
$(this).addClass("open");
} else if ($(this).hasClass("open")) {
$(this).removeClass("open");
$(this).next("ul").slideUp(350);
}
});
$("#nav > li:first > a").trigger('click');
//Or else you can go with below one too
//$("#nav li:first").find('a').trigger('click');
});
#2
2
Just trigger click on the first item. You can use :first
Pseudo Class
只需触发点击第一项即可。您可以使用:first Pseudo Class
$(document).ready(function() {
$("#nav > li > a").on("click", function(e) {
if ($(this).parent().has("ul")) {
e.preventDefault();
}
if (!$(this).hasClass("open")) {
// hide any open menus and remove all other classes
$("#nav li ul").slideUp(350);
$("#nav li a").removeClass("open");
// open our new menu and add the open class
$(this).next("ul").slideDown(350);
$(this).addClass("open");
} else if ($(this).hasClass("open")) {
$(this).removeClass("open");
$(this).next("ul").slideUp(350);
}
});
$("#nav > li:first > a").trigger('click');
});
/* nav menu styles */
#nav {
display: block;
width: 280px;
margin-left: -40px;
list-style-type: none;
}
#nav li {} #nav > li > a {
display: block;
padding: 8px 7px;
font-size: 1.3em;
font-weight: 200;
color: #889599;
text-decoration: none;
border-bottom: 1px solid #EFEFEF;
background-color: #F7F9FA;
/*background: -webkit-gradient(linear, left top, left bottom, from(#343434), to(#292929));
background: -webkit-linear-gradient(top, #343434, #292929);
background: -moz-linear-gradient(top, #343434, #292929);
background: -ms-linear-gradient(top, #343434, #292929);
background: -o-linear-gradient(top, #343434, #292929);
background: linear-gradient(top, #343434, #292929);*/
}
#nav > li > a:hover,
#nav > li > a.open {
color: #889599;
background-color: #F7F9FA;
}
#nav li ul {
display: none;
background: #EFEFEF;
list-style-type: none;
margin-left: -40px;
}
#nav li ul li a {
display: block;
background: none;
padding: 10px 0px;
padding-left: 30px;
font-size: 1.1em;
text-decoration: none;
font-weight: 200;
color: #889599;
}
#nav li ul li a:hover {
background: #F7F9FA;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav>
<ul id="nav">
<li><a href="#">Animation</a>
<ul>
<li><a href="http:/www.google.com/search?q=design+cartoons+animation">Cartoons</a>
</li>
<li><a href="http:/www.google.com/search?q=design+comic+strips+inspiration">Comic Strips</a>
</li>
<li><a href="http:/www.google.com/search?q=how+to+clip+video+footage">Video Clips</a>
</li>
<li><a href="http:/www.google.com/search?q=design+create+animated+gifs">Web GIFs</a>
</li>
</ul>
</li>
<li><a href="#">Graphic Design</a>
<ul>
<li><a href="http:/www.google.com/search?q=photoshop+tutorials+graphics+design">Adobe Photoshop</a>
</li>
<li><a href="http:/www.google.com/search?q=digital+branding+graphics+logos">Branding & Logos</a>
</li>
<li><a href="http:/www.google.com/search?q=graphics+design+marketing">Digital Marketing</a>
</li>
<li><a href="http:/www.google.com/search?q=graphic+design+illustrations">Illustrations</a>
</li>
<li><a href="http:/www.google.com/search?q=infographics+inspiration">Infographics</a>
</li>
<li><a href="http:/www.google.com/search?q=product+design+inspiration">Product Design</a>
</li>
</ul>
</li>
<li><a href="#">Digital Photography</a>
<ul>
<li><a href="http:/www.google.com/search?q=cityscape+photography">Cityscapes</a>
</li>
<li><a href="http:/www.google.com/search?q=water+ocean+photography">Oceans</a>
</li>
<li><a href="http:/www.google.com/search?q=wide+angle+lens+photography">Wide-Angle Lens</a>
</li>
<li><a href="http:/www.google.com/search?q=wildlife+photography">Wildlife</a>
</li>
</ul>
</li>
<li><a href="#">Print & Identity</a>
<ul>
<li><a href="http:/www.google.com/search?q=branding+identity+print+design">Branding</a>
</li>
<li><a href="http:/www.google.com/search?q=business+cards+design">Business Cards</a>
</li>
<li><a href="http:/www.google.com/search?q=print+design+letterpress">Letterpress</a>
</li>
<li><a href="http:/www.google.com/search?q=print+poster+artwork">Poster Art</a>
</li>
</ul>
</li>
<li><a href="#">Programming</a>
<ul>
<li><a href="http:/www.google.com/search?q=learn+css3+web+development">CSS3</a>
</li>
<li><a href="http:/www.google.com/search?q=learn+html5+web+development">HTML5</a>
</li>
<li><a href="http:/www.google.com/search?q=javascript+jquery+tutorials">JavaScript & jQuery</a>
</li>
<li><a href="http:/www.google.com/search?q=web+development+mysql">MySQL Databases</a>
</li>
<li><a href="http:/www.google.com/search?q=wordpress+programming">Wordpress CMS</a>
</li>
</ul>
</li>
<li><a href="#">Web Design</a>
<ul>
<li><a href="http:/www.google.com/search?q=web+design+icons">Icons</a>
</li>
<li><a href="http:/www.google.com/search?q=web+design+tutorials">Tutorials</a>
</li>
<li><a href="http:/www.google.com/search?q=web+design+user+interface">User Interfaces</a>
</li>
<li><a href="http:/www.google.com/search?q=web+design">Website Layouts</a>
</li>
</ul>
</li>
</ul>
</nav>
#3
0
One solution is that you can hard code in html to open that menu
一种解决方案是您可以在html中硬编码以打开该菜单
add open class to a
and style='display:block'
to your ul
将open类添加到a并将style ='display:block'添加到ul
<ul id="nav">
<li><a class="open" href="#">Animation</a>
<ul style="display: block;">
<li><a href="http:/www.google.com/search?q=design+cartoons+animation">Cartoons</a>
</li>
<li><a href="http:/www.google.com/search?q=design+comic+strips+inspiration">Comic Strips</a>
</li>
Or
You can add this before your click event
您可以在点击事件之前添加此项
$("nav").find("li:first a").addClass("open");
$("nav").find("li:first ul").show();