一、開發(fā)小要點(diǎn)
web頁(yè)面中,我們一般不用select、option來實(shí)現(xiàn)下拉菜單效果,因?yàn)橄吕虻臉邮匠笄译y以美化,所以我們選擇控制ul顯示隱藏來實(shí)現(xiàn)同樣且高大上的效果,但是不能像下拉框那樣點(diǎn)擊頁(yè)面其他部分,下拉菜單收起或隱藏,該怎么辦呢?只能用js這老大哥來控制了。
二、代碼
HTML:
<div class="select_box" id="selected"> <div class="select"> <span>請(qǐng)選擇</span> </div> <ul class="list"> <li>01</li> <li>02</li> <li>03</li> <li>04</li> </ul> </div>
CSS:
<style type="text/css"> *{margin:0;padding:0} ul,ol{list-style: none} .select_box{ position:relative; margin:100px auto; width:300px; } .select{ padding:5px 10px; border:1px solid #dedede; } .select:hover{ cursor:pointer; } .select span{ display: block; background:url("../../img/downicon.png") no-repeat right; } .list{ display: none; position:absolute; top:30px; width:298px; border:1px solid #dedede; border-top:none; } .list li{ padding:5px 10px; } .list li:hover{ background:#ddd; } </style>JS:
$(function(){ $(".select").click(function(){ $(".list").toggle(); }) $(".list li").click(function(){ $(".select span").html($(this).html()); $(".list").hide(); }) $(document).bind("click",function(e){ var e = e || window.event; //事件對(duì)象,兼容IE var target = e.target || e.srcElement; //源對(duì)象,兼容火狐和IE while(target){ if (target.id && target.id == "selected"){ //循環(huán)判斷至根節(jié)點(diǎn),防止點(diǎn)擊的是#selected和它的子元素 return; } target = target.parentNode; } $(".list").hide(); //點(diǎn)擊的不是#selected和它的子元素,隱藏下拉菜單 }) })效果:

總結(jié)
以上所述是小編給大家介紹的jQuery點(diǎn)擊頁(yè)面其他部分隱藏下拉菜單功能,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注