简体   繁体   English

显示/隐藏jQuery菜单单击功能

[英]Show/hide jQuery menu click functions

I have the following code for my menu, the trouble is I want it to hide/close as soon as I click on one of the options, instead of closing at the X button: 我的菜单有以下代码,麻烦的是我希望它在我单击选项之一时就隐藏/关闭,而不是在X按钮上关闭:

THIS IS THE HTML 这是HTML

MENU 菜单

<div class="mobilenav"> 
 <li><a href="#">HOME</a></li> 
 <li><a href="#">SERVICES</a></li> 
 <li><a href="#">WORK</a></li> 
<li><a href="#">TALK</a></li> 
</div>

ICON 图标

<a href="javascript:void(0)" class="icon"> 
<div class="MENU"> 
 <div class="menui top-menu"></div> 
 <div class="menui mid-menu"></div> 
 <div class="menui bottom-menu"></div> 
</div> 
</a>

AND JS 和JS

$(document).ready(function () {
$(".icon").click(function () {
    $(".mobilenav").fadeToggle(500);
    $(".top-menu").toggleClass("top-animate");
    $(".mid-menu").toggleClass("mid-animate");
    $(".bottom-menu").toggleClass("bottom-animate");
});

Try this: 尝试这个:

$( ".icon" ).bind( "click", function() {
    $(".mobilenav").fadeToggle(500);
    $(".top-menu").toggleClass("top-animate");
    $(".mid-menu").toggleClass("mid-animate");
    $(".bottom-menu").toggleClass("bottom-animate");
});

This will bind the click function to the class. 这会将click函数绑定到该类。 Also a side-note watch where you place your scripts. 还要注意放置脚本的旁注。 From personal experience I've had best results putting all js code (inline and external) right before the tag (even when using document.ready()) but this is probably just personal preference. 从个人经验来看,我将所有js代码(内联和外部)放在标签之前(即使在使用document.ready()时)也取得了最佳效果,但这可能只是个人喜好。

DOCUMENTATION FOR BIND 绑定文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM