简体   繁体   English

第二次和第三次点击时发生的事件

[英]Event on second and third click

I'm currently in the process of making a website for my band, and one idea I had was that on a specific page, you would see a picture of each band member, and when you hover over it with your mouse the picture would change and you would hear that band member saying something, then when you click on them, they say something else and you'll be redirected to their page. 我目前正在为乐队制作网站,当时的一个想法是,在特定页面上,您会看到每个乐队成员的图片,当您将鼠标悬停在该图片上时,图片会改变然后您会听到该乐队成员在说些什么,然后单击他们,他们说了些什么,您就会被重定向到他们的页面。

This alone isn't a problem, but for one member, I want it to take three clicks before you actually get redirected to his page; 仅此一项是没有问题的,但是对于一个成员,我希望它需要三下单击才能真正重定向到他的页面。 I also want him to say something different at each click. 我也希望他每次点击都说些不同的话。

So what I'm basically looking for is a way to create different events on the first, second and third click (preferably using javascript). 因此,我基本上想寻找的是一种在第一次,第二次和第三次点击(最好使用javascript)上创建不同事件的方法。

I hope you guys can help me out, thanks in advance! 希望大家能帮帮我,谢谢!

Just use variable to count clicks: 只需使用变量来计算点击次数:

var count = 0
$(".test").click(function() {
count++;
if(count == 1) {
        $(".test").text("first");
    }else if(count == 2){
        $(".test").text("second");
    }else if(count == 3){
        $(".test").text("third");
        count = 0;
    }    
})

http://jsfiddle.net/x83bf1gq/4/ http://jsfiddle.net/x83bf1gq/4/

An option could be to create an onclick handler that keeps saying things until all texts in an array of texts are said. 一种选择是创建一个onclick处理程序,该处理程序一直在说一些事情,直到说出一个文本数组中的所有文本为止。 Once all the texts have been said, you can just redirect or whatever action you need to be done. 说完所有文字后,您可以重定向或执行任何所需的操作。 Example: 例:

var johnTexts = [
    'hello',
    'how are you doing',
    'come on'
];
var jamesTexts = [
    'ready',
    'go'
];
var sayTexts = function (texts) {
    var i = 0;
    return function () {
        if (i < texts.length) {
            alert(texts[i++]);
        } else {
            alert('do your redirect or whatever you need');
        }
    };
}
document.getElementById('john').onclick = sayTexts(johnTexts);
document.getElementById('james').onclick = sayTexts(jamesTexts);

See demo 观看演示

Simply set an event listener that makes different actions based on a global counter of clicks. 只需设置一个事件侦听器,该事件侦听器将根据全局点击次数计数器做出不同的操作。 Check this fragment of code: 检查以下代码片段:

//set counter
var counter = 0; 
var component = document.getElementByID("ID-of-component");

component.addEventListener('mouseover', function(){
     //do something
});

component.addEventListener('click', function() {
     switch(++counter) { 
         case 1: /* do something */ break;
         case 2: /* do something */ break;
         case 3: /* do something */ break;
     }

     counter = 0; //reset counter
});

Obviously, you have to write this code for each component of your band. 显然,您必须为乐队的每个组件编写此代码。

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

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