为了账号安全,请及时绑定邮箱和手机立即绑定

求解:键盘事件只能执行一次

window.onload=function(){
        var box=document.getElementById('divselect'),
            title=box.getElementsByTagName('cite')[0],
            menu=box.getElementsByTagName('ul')[0],
            as=box.getElementsByTagName('a'),
            index=-1,
            flag=0;

        //键盘事件
        document.onkeyup=function(e){
            e=e||window.event;
            if(e.keyCode==13){
                menu.style.display='block';
                flag=1;
                document.onkeyup=function(e){
                    e=e||window.event;
                    if(e.keyCode==40){        //按下方向键
                        clear();
                        index++;
                        if(index>=as.length){
                            index=0;
                        }
                        change();
                    }

                    if(e.keyCode==38){        //按上方向键
                        clear();
                        index--;
                        if(index<0){
                            index=as.length-1;
                        }
                        change();
                    }
                    if(e.keyCode==13 && index!=-1){
                        title.innerHTML=as[index].innerHTML;
                        menu.style.display='none';
                        index=-1;
                    }else if(e.keyCode==13 && flag==1){
                        menu.style.display='none';
                        flag=0;
                    }
                    return flag,index;
                }
            }
            function clear(){
                for(var i= 0,l=as.length;i<l;i++){
                    as[i].style.background='#fff';
                    as[i].style.color='#333';
                }
            }
            function change(){
                as[index].style.background='#333';
                as[index].style.color='#fff';
            }

        }


        // 点击三角时
        title.onclick=function(event){

            //list粗线
            event=event||window.event;
            if(event.stopPropagation){
                event.stopPropagation();
            }else{
                event.cancelBubble=true;
            }
            menu.style.display='block';

            for(var i= 0,l=as.length;i<l;i++){         //滑过变色
                as[i].onmouseover=function(){
                    this.style.background='#333';
                    this.style.color='#fff';
                };
                as[i].onmouseout=function(){
                    this.style.background='#fff';
                    this.style.color='#333';
                };
                as[i].onclick=function(e){
                    e=e||window.event;
                    if(e.stopPropagation){
                        e.stopPropagation();
                    }else{
                        e.cancelBubble=true;
                    }
                    menu.style.display='none';              //list消失

                    title.innerHTML=this.innerHTML;         //改变标题
                }

            }

        }
        document.onclick=function(){
            menu.style.display='none';
        }

代码如上,用键盘操作一次后,再次按回车键就没反应了,请问是什么原因?

html和css均未修改

正在回答

1 回答

补充:鼠标问题没有问题

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

求解:键盘事件只能执行一次

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信