菜單切換不成功
求幫忙,菜單切換時出現問題,菜單切換的li標簽在鼠標經過時正常,但卻在鼠標移出后不正常顯示。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>淘寶搜索框</title>
<style>
@font-face{
font-family: iconfont;
src: url(http://g.tbcdn.cn/tb/icon-font/1.1.5/iconfont.eot);
}
*{
padding: 0;
margin: 0;
}
.search-{
margin-top: 10px;
}
a{
text-decoration: none;
font-size: 14px;
color: #999;
}
.search-tips{
float: right;
padding: 5px 10px 0 16px;
}
.search-button{
float: right;
}
.btn-search{
background: #F63;
width: 100px;
height: 45px;
color: #FFF;
font-size: 18px;
border: 0;
cursor: pointer;
text-align: center;
line-height: 45px;
}
.search-common-pannel input{
height: 39px;
line-height: 39px;
width: 100%;
border: 0 none;
outline: 0;
background: #FFF;
}
.search-common-pannel{
height: 39px;
background: #F50;
overflow: hidden;
padding: 3px 0 3px 76px;
}
ul{
list-style: none;
display: block;
}
.search-box{
position: absolute;
top: 13px;
left: 2px;
width: 74px;
height: 39px;
overflow: hidden;
background: #FFF;
display: block;
}
.search-box li{
display: none;
height: 39px;
line-height: 39px;
overflow: hidden;
text-align: center;
border: 1px solid #CCC;
border-top: none;
}
.search-box .selected{
background: #e5e5e5;
display: block;
}
.trigger-hover{
height: auto;
}
.trigger-hover li{
display: block;
}
</style>
</head>
<body>
<div class="search-">
<div id="search-tab" class="search-box">
? ? <ul>
? ? ? ? <li id="tab_1" class="selected">
? ? ? ? ? ? <a href="#">寶貝</a>
? ? ? ? ? ? </li>
? ? ? ? ? ? <li id="tab_2">
? ? ? ? ? ? <a href="#">店鋪</a>
? ? ? ? ? ? </li>
? ? ? ? </ul>
? ? </div>
? ? <div class="searchPannel">
? ? <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-pannel">
? ? ? ? ? ? <input type="text" x-webkit-speech="">
? ? ? ? ? ? <i></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;
if(el.addEventListener){
el.addEventListener(event, fn, false);
}else{
el.attachEvent('on'+event, fn);
}
}
addEvent('search-tab', 'mouseover', function(){
if(this.className.indexOf('trigger-hover') < 0){
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_2', 'mouseover', function(){
if(this.className.indexOf('selected') < 0){
this.className += 'selected';
}
});
addEvent('tab_2', 'mouseout', function(){
this.className = "";
});
addEvent('tab_2', 'click', function(){
getDOM('search-tab').className = 'search-box';
});
addEvent('tab_1', 'click', function(){
getDOM('search-tab').className = 'search-box';
});
</script>
</html>
2016-09-03
這樣就可以了,我也看了“搜索框制作”那教程,老師的代碼是有問題,自己花了點時間改了下。
2017-01-17
樓上的貌似還是有點小問題,不點擊,只移動鼠標,貌似會選中店鋪。我的代碼可以參照樓上用個函數簡化的,懶得搞了
2016-09-21
給樓上一個贊。