我正在做类似聊天对话的操作,您可以通过按空格键来显示下一条消息,但是它不起作用,有实时代码:

http://jsfiddle.net/VCpqs/7/

谁能解释如何通过按一个键到下一个消息来排序事件?

谢谢!

最佳答案

这是您要找的:http://jsfiddle.net/VCpqs/11/吗?

您需要跟踪当前正在显示的消息(为此,我使用类current),并相应地隐藏该消息并仅显示下一条消息。

这是jQuery:

$(document).keyup(function(event) {
    if (event.which === 32) {
        if ($('#msg1').hasClass('current'))
        {
            $('#msg1').hide('slow').removeClass('current');
            $('#msg2').show('slow').addClass('current');
        }
        else if ($('#msg2').hasClass('current'))
        {
            $('#msg2').hide('slow').removeClass('current');
            $('#msg3').show('slow').addClass('current');
        }
    }
});


很高兴知道您将其循环工作。我也在努力以循环方式实现它,这就是我得到的:http://jsfiddle.net/VCpqs/18/

var messages = new Array('msg1','msg2','msg3');

$(document).keyup(function(event) {
    if (event.which === 32) {
        for (x in messages)
        {
            if ($('#'+messages[x]).hasClass('current'))
            {
                $('#'+messages[x]).hide('slow').removeClass('current');
                $('#'+messages[(parseInt(x)+1)]).show('slow').addClass('current');
                break;
            }
        }
    }
});

08-06 13:05