简体   繁体   English

你是如何使jQuery需要第二次点击才能开始下一个事件,如果第一个使第二个事件成为真的话?

[英]How do you make jQuery require a second click to start the next event, if the first makes the second true?

I'm trying to write a code that will be able to flick between images on a website and I'm using JavaScript and jQuery. 我正在尝试编写一个能够在网站上的图像之间轻弹的代码,我正在使用JavaScript和jQuery。 I want it to move to the next of the three images when you click the button. 我希望它在您单击按钮时移动到三个图像中的下一个。 I've done this using .hide(); 我用.hide();做过这个.hide(); and .show(); .show(); to do this if a variable is true and when you .click() the button. if变量为true并且你.click()按钮,则执行此操作。

My problem is that it works for the first if statement, however the outcome of the first if statement makes the second one true, and so that runs as well, this then causes the third if statement to run, even though they are each within different .click() events. 我的问题是它适用于第一个if语句,但是第一个if语句的结果使第二个if语句成立,因此也会运行,这会导致第三个if语句运行,即使它们都在不同的范围内.click()事件。 I was wondering if there was any way of stopping the code running before you click the button again, or if there is a much easier way of coding something that changes between a few images. 我想知道是否有任何方法可以在再次单击按钮之前停止运行代码,或者是否有更简单的方法来编码在几个图像之间发生变化的内容。

Any help would be greatly appreciated, I'm pretty new to this and it is all a bit confusing! 任何帮助将不胜感激,我对此很新,这有点令人困惑!

Here's my code: 这是我的代码:

$(document).ready(function(){
                        $("#Image1").show();                     
                        var visbile1 = true;
                        $("#Image2").hide();
                        var visible2 = false;
                        $("#Image3").hide();
                        var visible3 = false;

                        $("#rightButton").click(function(){     
                                if (visible1 = true) {
                                $("#Image2").show();
                                $("#Image1").hide();
                                var visible3 = false;
                                var visible2 = true;
                                var visible1 = false;
                                };
                        });
                        $("#rightButton").click(function(event){
                                if (visible2 = true) {
                                $("#Image3").show();
                                $("#Image2").hide();
                                var visible3 = true;
                                var visible2 = false;
                                var visible1 = false;
                                };
                        }); 
                        $("#rightButton").click(function(){     
                                if (visible3 = true) {
                                $("#Image1").show();
                                $("#Image3").hide();
                                var visible3 = false;
                                var visible2 = false;
                                var visible1 = true;
                                };
                        });
                });             

Only bind 1 event with your 3 if and then the problem is solved : 只与你的3绑定1事件,然后问题解决:

$("#rightButton").click(function(){     
    if (visible1 = true) {
        $("#Image2").show();
        $("#Image1").hide();
        visible3 = false;
        visible2 = true;
        visible1 = false;
    }
    else if (visible2 = true) {
        $("#Image3").show();
        $("#Image2").hide();
        visible3 = true;
        visible2 = false;
        visible1 = false;
    }    
    else if (visible3 = true) {
        $("#Image1").show();
        $("#Image3").hide();
        visible3 = false;
        visible2 = false;
        visible1 = true;
    };
});

"or if there is a much easier way of coding something that changes between a few images." “或者如果有一种更简单的方法来编码在几张图像之间发生变化的东西。”

I won't do the code for you, but yes there is :) 我不会为你做代码,但是有的是:)

If it's confusing to you I would stick with a straightforward answer like the one Karl gave. 如果它让你感到困惑,我会坚持像Karl给出的一个直截了当的回答。 Studying that code should help you to learn and understand jQuery. 研究该代码应该有助于您学习和理解jQuery。 There are ways to make the code shorter as well. 有一些方法可以缩短代码。 Hopefully this example will be useful for you too. 希望这个例子也对你有用。

$(document).ready(function(){
  var counter = 0;
  var mod = 1;
  $("#Image1").show();                     
  $("#Image2").hide();
  $("#Image3").hide();

  $("#rightButton").click(function(){ 
       $("#Image"+mod).hide();                     
       counter++;
       mod = (counter % 3) + 1;
       $("#Image"+mod).show();
  });
});

暂无
暂无

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

相关问题 jQuery Click事件在第二次点击时触发,但第一次未触发 - Jquery Click Event fires on second click but not first jQuery点击事件在第二次点击时触发,但第一次未触发 - JQuery click event fires on second click but not on first jQuery:button_click事件在第一次单击时不触发,而在第二次单击时起作用,为什么? 如何解决? - jQuery : button_click event not fires on first click and works on second click, why? How to fix it? jQuery点击事件在第二次点击时触发,但在搜索表单中没有第一次触发 - JQuery click event fires on second click but not on first in search form 一秒钟内单击3的jQuery事件 - jQuery event on 3 click in one second 如何单击“下一步”按钮第一步检查验证,第二步不检查验证? - How do I click Next button First step check Validation and second step Not check Validation? 我如何制作一个点赞按钮,在第一次点击时增加计数器,在第二次点击时减少 - How do i make a like button that increase counter on first click and decrease on second click 如何为整个 class 按钮创建点击事件? jquery - How do you make a click event for an entire class of buttons? jquery “点击”事件仅在首次点击时触发,而在第二次点击时不触发 - 'click' event only fires on first click and not second 如何在第一次单击时执行操作并在第二次单击时执行另一个操作 - How to make an action on first click and another action on second click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM