zoukankan      html  css  js  c++  java
  • JS-事件之鼠标、键盘都能控制的下拉选框效果

    <script type="text/javascript">
    window.onload=function(e){
        var box=document.getElementById('divselect'),
            title=box.getElementsByTagName('cite')[0],
            menu=box.getElementsByTagName('ul')[0],
            as=box.getElementsByTagName('a'),//as是一个集合
            index=-1;
       
        // 点击三角时
        title.onclick=function(event){
            event = event || window.event;
            if(event.stopPropagation){
                event.stopPropagation();
            }else{
                event.cancelBubble = true;
            };//以上是阻止冒泡的判断语句
            menu.style.display = "block";
          
          //添加键盘事件
          //问题出在按了回车之后,怎么把对应的分类内容填进去。
          document.onkeyup = function(event){
            event = event || window.event;
    //        console.log(event.keyCode)//用这个方法获取到:回车是13,空格是32,上键是38,下键是40;
            /***********
            if(event.keyCode===32){
                alert('kg');
            }else if(event.keyCode===13){
                //看来必须要是三个等号才会成立。
                alert('hc');
            }
            ********/
            if(event.keyCode===40){
                index++;
                if(index > as.length-1){
                    index = 0
                };
                for(var i=0;i<as.length;i++){
                    as[i].style.backgroundColor = null;
                };
                as[index].style.backgroundColor = "#ccc";
            };
            if(event.keyCode===38){
                index--;
                if(index<0){
                    index = as.length-1;
                }
                for(var i=0;i<as.length;i++){
                    as[i].style.backgroundColor = null;
                };
                as[index].style.backgroundColor = "#ccc";
            }
            if(event.keyCode===13){
                for(var i=0;i<as.length;i++){
                    as[i].style.backgroundColor = null;
                };
                title.innerHTML = as[index].innerHTML;
                       menu.style.display = "none";
            }
         };
      };  
           //封装-命名函数
    // var overColor = function(){
    //         this.style.backgroundColor = "#ccc";
    // }
    //    function outColor(){
    //         this.style.backgroundColor = null;
    //     }
    //    function replaceInner(){
    //         title.innerHTML = this.innerHTML;
    //         menu.style.display = "none";
    //     }
       // 滑过滑过、离开、点击每个选项时
       for(var i=0;i<as.length;i++){
    //     as[i].onmouseover = overColor;
    //     as[i].onmouseout = outColor;
    //     as[i].onclick = replaceInner;
    //写法2——直接调用
        as[i].onmouseover = function(){
               this.style.backgroundColor = "#ccc";
               };
           as[i].onmouseout = function(){
               this.style.backgroundColor = null;
               };
           as[i].onclick = function(){
               title.innerHTML = this.innerHTML;
               menu.style.display = "none";
               };
        };//当实在找不到错误的时候,看看是不是不小心删掉了花括号啥的
       // 点击页面空白处时
       document.addEventListener('click',function(){
           //注意区分:attachEvent(ie中添加事件监听器的方法)和addEventListener(通用浏览器中添加事件监听器)。别搞混了写成attachEventListener了!!
           menu.style.display = "none";
       },false);
    
    }
       </script>
    <div id="divselect">
          <cite>请选择分类</cite>
          <ul>
             <li id="li"><a href="javascript:;" selectid="1">ASP开发</a></li>
             <li><a href="javascript:;" selectid="2">.NET开发</a></li>
             <li><a href="javascript:;" selectid="3">PHP开发</a></li>
             <li><a href="javascript:;" selectid="4">Javascript开发</a></li>
             <li><a href="javascript:;" selectid="5">Java特效</a></li>
          </ul>
        </div>
    html
    <style type="text/css">
    body,ul,li{ margin:0; padding:0; font-size:13px;}
    ul,li{list-style:none;}
    #divselect{width:186px; margin:80px auto; position:relative; z-index:10000;}
    #divselect cite{width:150px; height:24px;line-height:24px; display:block; color:#807a62; cursor:pointer;font-style:normal;
    padding-left:4px; padding-right:30px; border:1px solid #333333; 
    background:url(xjt.png) no-repeat right center;}
    #divselect ul{width:184px;border:1px solid #333333; background-color:#ffffff; position:absolute; z-index:20000; margin-top:-1px; display:none;}
    #divselect ul li{height:24px; line-height:24px;}
    #divselect ul li a{display:block; height:24px; color:#333333; text-decoration:none; padding-left:10px; padding-right:10px;}
        </style>

    任务
    一、 点击菜单中的向下三角展开菜单
    提示:
    1、点击三角时需阻止事件冒泡
    二、 展开菜单之后,在document对象上绑定keyup事件,(键盘事件不是某个具体的对象了,所以要帮到document上面来)按下向下方向键,选中下一个选项,按下向上方向键,选中上一个选项,按下回车键菜单收起,显示选中项
    提示:
    1、 声明一个全局的index变量初值为-1
    2、 按下向下方向键时index递增,当递增至大于等于菜单选项的总数时恢复为0
    3、 按下向上方向键时判断index,如若小于等于0则设为菜单选项的总数,之后递减index
    4、 根据index值将对应的选项设为当前(灰色背景)
    5、 按下回车键时将对应选中的选项设为菜单标题,且将所有选项设为无背景,index恢复为-1,菜单收起
    注意:没有任何选项被选中时,按下回车键不做任何操作
    三、鼠标滑过每个选项时高亮显示,离开时去掉背景,点击高亮选项时菜单标题改变
    提示:
    1.遍历所有a标签,绑定鼠标点击的事件
    注意:要考虑到浏览器兼容,使用innerHTML,不要使用innerText
    四、点击页面空白处收起菜单
    提示:
    1.绑定在document对象上

    慕课练习题:http://www.imooc.com/code/3631

  • 相关阅读:
    Spring+MyBatis
    MyBatis的关于批量数据操作的测试
    mysql插入数据后返回自增ID的方法,last_insert_id(),selectkey
    Java数据持久层框架 MyBatis之API学习五(Mapper XML 文件)
    Java数据持久层框架 MyBatis之API学习三(XML 映射配置文件)
    WebView高危接口安全检测
    Android studio Debug效率提升
    Android Bitmap圆角
    Android 瘦身攻略
    过滤Emoji表情😊
  • 原文地址:https://www.cnblogs.com/padding1015/p/5903215.html
Copyright © 2011-2022 走看看