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

我这段代码为什么没有实现切换效果?并且鼠标滑过第二个li时,第一个li还是处于高亮状态

<html>

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<title>taobao demo</title> 

<style type="text/css">

@font-face{font-family:iconfont;src:url(http://g.tbcdn.cn/tb/icon-font/1.1.5/iconfont.eot);

src:url(http://g.tbcdn.cn/tb/icon-font/1.1.5/iconfont.eot?#iefix) format("embedded-opentype"),url(http://g.tbcdn.cn/tb/icon-font/1.1.5/iconfont.woff) format("woff"),url(http://g.tbcdn.cn/tb/icon-font/1.1.5/iconfont.ttf) format("truetype"),

url(http://g.tbcdn.cn/tb/icon-font/1.1.5/iconfont.svg#uxiconfont) format("svg")}

body{font:12px/1.5 tahoma,arial,sans-serif;}

a{text-decoration:none;}

.search-container{position:relative;}

.search-tips{float:right; padding:3px 0 0 15px;}

.search-tips a{color:#6c6c6c;}

.search-button{float:right;}

.btn-search{

background:url(http://gtms01.alicdn.com/tps/i1/T1qyj8Fc8aXXc4E9bI-400-300.png);

width:100px;

height:45px;

background-position:0 -200px;

border:0;

cursor:pointer;

}

.search-common-panel{

height:39px;

background-color:#f50;

overflow:hidden;

padding:3px 0 3px 77px;

}

.search-common-panel input{

height:39px;

line-height:39px;

width:100%;

border:0 none;

outline:0;

background-color:#fff;

    }

    .search-common-panel i{

    position:absolute;

    top:14px;

    left:86px;

    z-index:2;

    color:#ccc;

    } 

    .iconfont{

    font-family:iconfont;

    font-size:12px;

    font-style:normal;

    } 

    ul{

    list-style:none;

    display:block;

    }

    ul,li{

    margin:0;

    padding:0;

    }

    .search-list{

    position:absolute;

    top:3px;

    left:3px;

    width:72px;

    height:39px;

    overflow:hidden;

    background:#fff;

    border-left:1px solid #f6f6f6;

    border-right:1px solid #e5e5e5;

    }

    .search-list li{

    display:none;

    height:39px;

    line-height:39px;

    overflow:hidden;

    text-align:center;


    }

    .search-list li a{

    color:#6c6c6c;

    }

    .search-list .selected{

    background:#f6f6f6;

    display:block;

    }

    .trigger-hover{

    height:auto;

    }

    .trigger-hover li{

    display:block;

    }

</style>

</head>

<body>

<div class="search-container">

<div id="search_tab" class="search-list">

<ul>

<li id="tab_1" class="selected"><a href="#">宝贝</a></li>

<li id="tab_2"><a href="#">店铺</a></li>

</ul>

</div>

<div class="search-pannel">

<form>

  <div class="search-tips">

  <a href="#">

  高级<br>搜索

  </a>

  </div>

  <div class="search-button">

      <button class="btn-search" type="submit"></button>

  </div>

  <div class="search-common-panel">

  <input type="text" x-webkit-speech="">

  <i class="iconfont">ő</i>

  </div>

</form>

</div>

</div>

</body>

<script>

var getDom=function(id){

return document.getElementById(id);

}

var addEvent=function(id,event,fn){

var el=getDom(id)||document;//为防止获取不存在的id而报错

if(el.addEventListener){ //针对非IE浏览器的环境

        el.addEventListener(event,fn,false);//false为默认

}else if(el.attachEvent){ //针对IE浏览器的环境

   el.attachEvent('on'+event,fn);

}

}

addEvent('search_tab','mouseover',function(){

this.className+=' trigger-hover';

});


addEvent('tab_1','mouseover',function(){

if(this.className.indexOf('selected')<0){

this.className+='selected';

}

});

addEvent('tab_1','mouseout',function(){

this.className+='';

})

addEvent('tab_1','click',function(){

getDom('search_tab').className='search-list';

});

addEvent('tab_2','click',function(){

getDom('search_tab').className='search-list';

});


addEvent('tab_2','mouseover',function(){

if(this.className.indexOf('selected')<0){

this.className+='selected';

}

});

addEvent('tab_2','mouseout',function(){

this.className+='';

})



</script>


</html>


正在回答

3 回答

<script>
 var getDOM=function(id){
     return document.getElementById(id);
     }
 var addEvent=function(id,event,fn){
     var el=getDOM(id)||document;
     if(el.addEventListener){
         el.addEventListener(event,fn,false);
         }else if(el.attachEvent){
             el.attachEvent('on'+event,fn);
             }
     }
 addEvent('search-tab','mouseover',function(){
     this.className +=' trigger-hover';//一定要有空格
     })
 addEvent('search-tab','mouseout',function(){
     this.className ='search-list';
     })
     
 addEvent('tab_1','mouseover',function(){
     if(this.className.indexOf('selected')<0){
         this.className ='selected';
         }
     });
 addEvent('tab_1','mouseout',function(){
         this.className='';
     });
 addEvent('tab_1','click',function(){
         getDOM('search-tab').className ='search-list';
     });
addEvent('tab_2','mouseover',function(){
     if(this.className.indexOf('selected')<0){
         this.className ='selected';
         }
     });
 addEvent('tab_2','mouseout',function(){
         this.className =' ';
     });
  addEvent('tab_2','click',function(){
         this.className =' ';//鼠标滑过时已经将tab_2的className 设置为'selected' 这里置空。
         var a =getDOM('tab_1').innerHTML;
         getDOM('tab_1').innerHTML=getDOM('tab_2').innerHTML;
         getDOM('tab_2').innerHTML=a;
         getDOM('search-tab').className ='search-list';
     });
</script>

这是我的代码,希望对你有帮助

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

<html>

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<title>taobao demo</title> 

<style type="text/css">

@font-face{font-family:iconfont;src:url(http://g.tbcdn.cn/tb/icon-font/1.1.5/iconfont.eot);

src:url(http://g.tbcdn.cn/tb/icon-font/1.1.5/iconfont.eot?#iefix) format("embedded-opentype"),url(http://g.tbcdn.cn/tb/icon-font/1.1.5/iconfont.woff) format("woff"),url(http://g.tbcdn.cn/tb/icon-font/1.1.5/iconfont.ttf) format("truetype"),

url(http://g.tbcdn.cn/tb/icon-font/1.1.5/iconfont.svg#uxiconfont) format("svg")}

body{font:12px/1.5 tahoma,arial,sans-serif;}

a{text-decoration:none;}

.search-container{position:relative;}

.search-tips{float:right; padding:3px 0 0 15px;}

.search-tips a{color:#6c6c6c;}

.search-button{float:right;}

.btn-search{

background:url(http://gtms01.alicdn.com/tps/i1/T1qyj8Fc8aXXc4E9bI-400-300.png);

width:100px;

height:45px;

background-position:0 -200px;

border:0;

cursor:pointer;

}

.search-common-panel{

height:39px;

background-color:#f50;

overflow:hidden;

padding:3px 0 3px 77px;

}

.search-common-panel input{

height:39px;

line-height:39px;

width:100%;

border:0 none;

outline:0;

background-color:#fff;

    }

    .search-common-panel i{

    position:absolute;

    top:14px;

    left:86px;

    z-index:2;

    color:#ccc;

    } 

    .iconfont{

    font-family:iconfont;

    font-size:12px;

    font-style:normal;

    } 

    ul{

    list-style:none;

    display:block;

    }

    ul,li{

    margin:0;

    padding:0;

    }

    .search-list{

    position:absolute;

    top:3px;

    left:3px;

    width:72px;

    height:39px;

    overflow:hidden;

    background:#fff;

    border-left:1px solid #f6f6f6;

    border-right:1px solid #e5e5e5;

    }

    .search-list li{

    display:none;

    height:39px;

    line-height:39px;

    overflow:hidden;

    text-align:center;


    }

    .search-list li a{

    color:#6c6c6c;

    }

    .search-list .selected{

    background:#f6f6f6;

    display:block;

    }

    .trigger-hover{

    height:auto;

    }

    .trigger-hover li{

    display:block;

    }

</style>

</head>

<body>

<div class="search-container">

<div id="search_tab" class="search-list">

<ul>

<li id="tab_1" class="selected"><a href="#">宝贝</a></li>

<li id="tab_2"><a href="#">店铺</a></li>

</ul>

</div>

<div class="search-pannel">

<form>

  <div class="search-tips">

  <a href="#">

  高级<br>搜索

  </a>

  </div>

  <div class="search-button">

      <button class="btn-search" type="submit"></button>

  </div>

  <div class="search-common-panel">

  <input type="text" x-webkit-speech="">

  <i class="iconfont">ő</i>

  </div>

</form>

</div>

</div>

</body>

<script>

var getDom=function(id){

return document.getElementById(id);

}

var addEvent=function(id,event,fn){

var el=getDom(id)||document;//为防止获取不存在的id而报错

if(el.addEventListener){ //针对非IE浏览器的环境

        el.addEventListener(event,fn,false);//false为默认

}else if(el.attachEvent){ //针对IE浏览器的环境

   el.attachEvent('on'+event,fn);

}

}

addEvent('search_tab','mouseover',function(){

this.className+=' trigger-hover';

});


addEvent('tab_1','mouseover',function(){

if(this.className.indexOf('selected')<0){

this.className+=' selected';

}

});

addEvent('tab_1','mouseout',function(){

this.className='';

})

addEvent('tab_1','click',function(){

getDom('search_tab').className='search-list';

});

addEvent('tab_2','click',function(){

getDom('search_tab').className='search-list';

});


addEvent('tab_2','mouseover',function(){

if(this.className.indexOf('selected')<0){

this.className+=' selected';

}

});

addEvent('tab_2','mouseout',function(){

this.className='';

})



</script>


</html>

我改成这样了,但是切换的时候,选择店铺后,变成空白的,并且就算选择宝贝,也不能正常的隐藏店铺,这什么情况

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

慕课此昵称

解决了吗?我也是同样的问题。
2016-08-13 回复 有任何疑惑可以回复我~

addEvent('tab_1','mouseout',function(){

this.className+='';

})

addEvent('tab_2','mouseout',function(){

this.className+='';

})

中的this.className+='';改为this.className='';

addEvent('tab_1','mouseover',function(){

if(this.className.indexOf('selected')<0){

this.className+='selected';

}

});

addEvent('tab_2','mouseover',function(){

if(this.className.indexOf('selected')<0){

this.className+='selected';

}

});

中的this.className+='selected';改为this.className+=' selected';添加空格,要不然成为一个类search-listselected

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

慕妹7683697 提问者

我改成这样了,但是切换的时候,选择店铺后,变成空白的,并且就算选择宝贝,也不能正常的隐藏店铺,这什么情况(具体代码我发在楼下了,能帮忙看下吗,谢谢了
2016-08-01 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
搜索框制作
  • 参与学习       66041    人
  • 解答问题       431    个

本课程从简入深讲解搜索框的制作,学习JQ与JS实现Ajax技术的不同点

进入课程

我这段代码为什么没有实现切换效果?并且鼠标滑过第二个li时,第一个li还是处于高亮状态

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