li > a").on("clic-6ren">
gpt4 book ai didi

jquery - 在 Jquery 中加载页面时展开第一个菜单选项

转载 作者:行者123 更新时间:2023-12-01 03:10:45 26 4
gpt4 key购买 nike

我创建了一个工作正常的菜单。但在页面加载时,所有菜单项都折叠起来。看看我的代码

$(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>&nbsp;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>&nbsp;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 &amp; 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>&nbsp;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>&nbsp;Print &amp; 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>&nbsp;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 &amp; 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>&nbsp;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>

页面加载时,所有菜单项都会折叠。我只想在页面加载时展开第一个菜单选项。

请帮我创建这个。

最佳答案

只需这样做:

$(document).ready(function(){
$("#nav li:first").find('a').trigger('click');
})

<罢工>

更新

<强> 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');
});

关于jquery - 在 Jquery 中加载页面时展开第一个菜单选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31738185/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com