简体   繁体   English

jquery 事件处理程序仅运行一次

[英]jquery event handler only running once

I am having problems with jqueries "click" event handler, its working only once, and then it stops, please I need help fixing it.我在使用 jqueries “click”事件处理程序时遇到问题,它只工作一次,然后就停止了,我需要帮助来修复它。

What I did is, I use an image as a button, for my Web app but, through jquery, but it only runs once.我所做的是,我使用图像作为按钮,用于我的 Web 应用程序,但是通过 jquery,但它只运行一次。

Here is the code:这是代码:

$( document ).ready(function refresh() {
    $(".menu_button").click(() => {
        $("nav").addClass("open-menu");
    });
});

$( document ).ready(function() {
    $(".cancel_menu").click(() => {
        $("nav").removeClass("open-menu").css("transform", "translatex(100%)").css("transition-duration", "2s")
    });
});

Please tell me if you need more info, Thanks.如果您需要更多信息,请告诉我,谢谢。

The problem I don't think is with the jquery.我认为问题不在于 jquery。 Your cancel menu moves the menu all the way to the right, but you never bring it back.你的取消菜单将菜单一直向右移动,但你永远不会把它带回来。

I reset the transform to none on the click of the menu button and it works.我在单击菜单按钮时将转换重置为无,它可以工作。

Below is the relevant line in the menu_button click event handler下面是 menu_button 单击事件处理程序中的相关行

$("nav").addClass("open-menu").css("transform", "none");

 $( document ).ready(function refresh() { $(".menu_button").click(() => { $("nav").addClass("open-menu").css("transform", "none"); }); }); $( document ).ready(function() { $(".cancel_menu").click(() => { $("nav").removeClass("open-menu").css("transform", "translatex(100%)").css("transition-duration", "2s") }); });
 .open-menu{ background:red; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button class="menu_button">OPEN</button> <button class="cancel_menu">CANCEL</button> <nav>NAV</nav>

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

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