元素id不变

时间:2012-11-30 07:48:59

标签: javascript html5 css3

我有一个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/

0 个答案:

没有答案