简体   繁体   English

jquery 拨动开关仅工作一次且不关闭

[英]jquery toggle is working only one time and not closing back

I'm new to js and jquery, I'm trying to open and close some menu, and it does open on first click but it is not closing after I click again.我是 js 和 jquery 的新手,我正在尝试打开和关闭一些菜单,它在第一次单击时会打开,但在我再次单击后它不会关闭。

    $(document).ready(function(){
        $("#toggle-menu").click(function(){
        $("#btn").toggle(1000);
    });

another weird thing is that if I click before the 1 second toggle end, it does work and closing另一个奇怪的事情是,如果我在 1 秒切换结束之前单击,它确实可以工作并关闭

                <div id="toggle-menu" ">
                    <input type="checkbox" id="check">
                    <label for="check" onclick="event.stopPropagation();">
                        <i class="fa fa-bars fa-2x i_dynm" aria-hidden="true"></i>
                    </label>
                </div>

                <div id="btn">
                    <li id="btnli">
                        <button>
                            <i class="fa fa-user-circle-o" aria-hidden="true"></i>
                            something
                        </button>
                    </li>
                    <li><button class="mb">something</button></li>
                    <li>
                        <button>
                            <i class="fa fa-question-circle" aria-hidden="true"></i>something else
                        </button>
                    </li>
                </div>

I'd suggest going through the jQuery docs once.我建议浏览一次 jQuery 文档。 Also, the code above will throw an error as the .click(function(){ is incomplete. I'd also suggest using .on instead.此外,上面的代码将引发错误,因为.click(function(){不完整。我还建议使用.on代替。

 $(document).ready(function(){ $("#toggle-menu").on('click', function(){ $("#btn").toggle(1000); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="toggle-menu"> Toggle </button> <button id="btn"> Button </button>

Ok, you might have to shed more details on what you want to achieve.好的,您可能需要详细说明您想要实现的目标。 I see that you are basing it on a checkbox?我看到你是基于一个复选框? May I ask why?我可以问为什么吗? Probably not the best way to handle click event on a container with a clickable item inside.可能不是处理内部带有可点击项目的容器上的点击事件的最佳方式。 I'd suggest binding to the checkbox or using a button instead.我建议绑定到复选框或使用按钮。 But anyway, you might have to use slideUp and SlideDown instead and check that the checkbox is checked.但无论如何,您可能必须改用 slideUp 和 SlideDown 并检查复选框是否已选中。 With the code below, it will remember how many click you've done and show/hide menu that many times.使用下面的代码,它会记住您点击了多少次,并显示/隐藏了多少次菜单。 You can add another check and disable click until the animation is completed.您可以添加另一个检查并禁用单击,直到 animation 完成。

 $(document).ready(function(){ $("#toggle-menu").click(function(){ var isChecked = $("#check").is(":checked"); if(isChecked){ $("#btn").slideDown(1000); }else{ $("#btn").slideUp(1000); } }); });
 #btn{ display:none; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="toggle-menu"> <input type="checkbox" id="check"> <label for="check" onclick="event.stopPropagation();"> <i class="fa fa-bars fa-2x i_dynm" aria-hidden="true"></i> </label> </div> <div id="btn"> <li id="btnli"> <button> <i class="fa fa-user-circle-o" aria-hidden="true"></i> something </button> </li> <li><button class="mb">something</button></li> <li> <button> <i class="fa fa-question-circle" aria-hidden="true"></i>something else </button> </li> </div>

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

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