0

我有一个绑定到键的 id 更改,但由于某种原因,它始终无法运行 - 尽管其他更改在键绑定中运行完美,并且用于更改 id 的代码与程序中其他地方的功能代码相同。

相关代码:

var KeyBind = function() {
    if (document.addEventListener) {
        document.addEventListener("keydown", keyDownHandler, false);
        document.addEventListener("keydown", keyUpHandler, false);
    }
    else if (document.attachEvent) {
        document.attachEvent("onkeydown", function() {
            keyDownHandler(window.event);
        });
        document.attachEvent("onkeydown", function() {
            keyUpHandler(window.event);
        });
    }

    function keyDownHandler(e) {
        var key = e.which || e.keyCode
        if (key == "50"){
            document.getElementById('Player').id='PlayerLeft';
            document.getElementById('PlayerLeft').style.webkitAnimationPlayState="running";
            setTimeout(function(){
                document.getElementById('PlayerLeft').style.webkitAnimationPlayState="paused";
            }, 200);
        }

        if (key == "49"){
            document.getElementById('PlayerLeft').style.webkitAnimationPlayState="running";
            setTimeout(function(){
                document.getElementById('PlayerLeft').style.webkitAnimationPlayState="paused";
            }, 800);                
        };

        if (key == "57"){
            //setTimeout(function(){
            document.getElementById('PlayerLeft2').style.webkitAnimationPlayState="running";
            //}, 800);

            setTimeout(function(){
                document.getElementById('PlayerLeft2').style.webkitAnimationPlayState="paused";
            }, /*1*/350);

            /*setTimeout(function(){
                document.getElementById('PlayerLeft2').style.webkitAnimationPlayState="running";
            }, 1150);

            setTimeout(function(){
                document.getElementById('PlayerLeft2').id='Player';                 
            }, 1500);*/         
        };

        if (key == "57" && document.getElementById('PlayerLeft2').style.left>="681px"){
            document.getElementById('PlayerLeft2').style.webkitAnimationPlayState="running";
            var idChanger = function(){
                document.getElementById('PlayerLeft2').id='Player';
            };
            setTimeout(//function(){                    
                idChanger
            /*}*/, 100);
        };                                                                              
    };
}();

jsfiddle:http: //jsfiddle.net/6JUaB/

4

0 回答 0