<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

相关文章:

  • 2021-10-01
  • 2021-07-22
  • 2022-02-08
  • 2021-11-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-07
  • 2022-12-23
  • 2022-01-06
  • 2021-10-25
  • 2022-12-23
相关资源
相似解决方案