到目前为止,我有以下代码:
<div id="wrapper">
<div id="user_info">
<p>Signed in as <span>max</span></p>
<ul>
<li><a href="#">Help</a></li>
<li id="siteSelector"><a href="#" class="">Switch to▼</a>
<ul>
<li><a href="JavaScript:void(0);">Item 1</a></li>
<li><a href="JavaScript:void(0);">Item 2</a></li>
<li><a href="JavaScript:void(0);">Item 3</a></li>
<li><a href="JavaScript:void(0);">Item 4</a></li>
</ul>
</li>
<li><a href="#">Sign Out</a></li>
</ul>
</div>
</div>
CSS
#wrapper {
width: 1200px;
}
#user_info {
margin: 0 auto;
text-align: right;
width: 250px;
}
#user_info p {
color: #333333;
margin-bottom: 0;
margin-right: 1.6em;
}
#user_info ul {
margin-top: 5px;
}
#user_info ul li {
float: left;
list-style-type: none;
padding: 5px 3px;
}
#user_info a {
color: #0B594A;
}
#siteSelector {
position: relative;
}
#siteSelector > a {
padding: 0 10px;
}
#siteSelector ul {
display: none;
}
#siteSelector ul li {
text-align: left;
width: 95%;
}
.siteSelect_anchor {
background: none repeat scroll 0 0 #0B594A;
border-radius: 6px 6px 0 0;
color: #FFFFFF !important;
display: block;
margin-top: -5px;
padding-top: 5px !important;
text-decoration: none;
}
.siteSelect_ul {
background: none repeat scroll 0 0 #FFFFFF;
border-color: #0B594A;
border-radius: 6px 6px 6px 6px;
border-style: solid;
border-width: 5px;
box-shadow: 0 5px 20px #666666;
display: block !important;
height: auto;
left: 0;
padding-bottom: 15px;
position: absolute;
top: 20px;
width: 59px;
z-index: 250;
}
.siteSelect_ul a {
margin-left: -30px;
}
和 JS:
function clickNav() {
$("#siteSelector").click(function() {
$("a:first", this).toggleClass('siteSelect_anchor');
$("ul:first", this).toggleClass('siteSelect_ul');
$(".siteSelect_ul li").click(function(e) {
var liTxt = $(this, ">a").text();
$("#siteSelector > a").text(liTxt);
e.stopPropagation();
});
$(document).click(function() {
$("a:first", this).removeClass('siteSelect_anchor');
$("ul:first",this).removeClass('siteSelect_ul');
});
});
}
单击即可打开下拉菜单,但仅当您再次单击时才会关闭#siteSelector
。如果有人单击页面上的任何位置,我需要关闭它,这就是我使用它的原因,$(document).click(function)
但它不起作用,我不知道为什么。有任何想法吗?