[英]jQuery get class of selected element
I have a bunch of divs that are buttons on my site. 我有一堆的div是我网站上的按钮。 When I click a button I want a sub nav to show.
当我单击一个按钮时,我希望显示一个子导航。 All the sub navs have the class of "subNab" but also have a class the same as the main nav appended with "Sub".
所有子导航都具有“ subNab”类,但也具有与附加“ Sub”的主导航相同的类。 For example I have:
例如,我有:
<div class="navBox audits">
<p>Audits</p>
</div>
<div class="subNav auditsSub">
<div class="navBox">
<p>Audits Sub1</p>
</div>
<div class="navBox">
<p>Audits Sub2</p>
</div>
</div>
Currently I have the sub nav hiding on page load and showing on a click function, along with hiding the subNav again so they don't just stack up. 目前,我在页面加载时隐藏了子导航,并在单击功能上显示了该子导航,并再次隐藏了子导航,因此它们不会堆积在一起。 I have the below code with the different class names for each button.
我在下面的代码中为每个按钮使用了不同的类名。 There's got to be an easier way.
一定有一种更简单的方法。
$(".audits").click(function(){
$(".subNav").hide();
$(".auditsSub").fadeIn("slow");
$("#adSlider").hide();
});
$(".billing").click(function(){
$(".subNav").hide();
$(".billingSub").fadeIn("slow");
$("#adSlider").hide();
});
$(".consulting").click(function(){
$(".subNav").hide();
$(".consultingSub").fadeIn("slow");
$("#adSlider").hide();
});
Thanks in advance! 提前致谢!
Can you give me the full code so I can see exactly how you want to handle your menu. 您能给我完整的代码,以便我确切地看到您要如何处理菜单。 If you organize your code correctly you don't have to give each trigger button a class.
如果您正确地组织了代码,则不必为每个触发按钮提供一个类。 Maybe this is what you are looking for: $(document).ready( function() {
也许这就是您要寻找的:$(document).ready(function(){
$(document).ready(function(){
$('.submenu').hide();
$('.menu ul li a').click(function(event){
event.preventDefault(); // prevents link from redirrecting
$('.submenu').hide(); // hide all submenu items
$(this).siblings('.submenu').slideDown (); // show only child submenu
});
}); });
http://jsfiddle.net/75d67hLq/1/ http://jsfiddle.net/75d67hLq/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.