為什么這里鼠標按下切換選項時不需要阻止冒泡,而老師講的QQ面板的例子里就需要阻止冒泡?
<!doctype?html> <html> <head> ????<meta?charset="UTF-8"> ????<title>下拉菜單</title> ?<style?type="text/css"> body,ul,li{?margin:0;?padding:0;?font-size:13px;} ul,li{list-style:none;} #divselect{width:186px;?margin:80px?auto;?position:relative;?z-index:10000;} #divselect?cite{width:150px;?height:24px;line-height:24px;?display:block;?color:#807a62;?cursor:pointer;font-style:normal; padding-left:4px;?padding-right:30px;?border:1px?solid?#333333;? background:url(xjt.png)?no-repeat?right?center;} #divselect?ul{width:184px;border:1px?solid?#333333;?background-color:#ffffff;?position:absolute;?z-index:20000;?margin-top:-1px;?display:none;} #divselect?ul?li{height:24px;?line-height:24px;} #divselect?ul?li?a{display:block;?height:24px;?color:#333333;?text-decoration:none;?padding-left:10px;?padding-right:10px;} ?</style> ???<script?type="text/javascript"> window.onload=function(){ ?var?box=document.getElementById('divselect'), ?????title=box.getElementsByTagName('cite')[0], ?????menu=box.getElementsByTagName('ul')[0], ?????as=box.getElementsByTagName('a'), ????????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){ ??for(var?i=0;i<as.length;i++){ ????as[i].style.background='#fff'; ??} ??if(event.keyCode==40){ ???index++; ???if(index>as.length){ ????index=0; ???} ???as[index].style.background='#567'; ??}else?if(event.keyCode==38){ ???index--; ???if(index<0){ ????index=as.length; ???} ???as[index].style.background='#567'; ??} ??if(index!=-1&&event.keyCode==13){ ???title.innerHTML=as[index].innerHTML; ???menu.style.display='none'; ???index=-1; ??} ?} ???//?滑過滑過、離開、點擊每個選項時 ??????//?執行腳本 ???for(var?i=0;i<as.length;i++){ ????as[i].onmouseover=function(){ ?????this.style.background='#567'; ????} ????as[i].onmouseout=function(){ ?????this.style.background='#fff'; ????} ????as[i].onclick=function(){ ?????title.innerHTML=this.innerHTML; ?????menu.style.display='none'; ????} ?} ??? ???//?點擊頁面空白處時 ???????//?執行腳本 ????document.onclick=function(){ ?????menu.style.display='none'; ?} ?} ???</script> </head> <body> ?<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> </body> </html>
上面是我寫的,還有一個問題:
if(index!=-1&&event.keyCode==13){ ???title.innerHTML=as[index].innerHTML; ???menu.style.display='none'; ???index=-1; ??}
這個if語句里面還需要清除背景嗎?document.onkeyup=function(event){}里一開始就清除背景了啊。我看到老師給的答案里有清除背景的for循環。
求大神快來解答啊?。?!
2016-04-20
?//?點擊三角時
????title.onclick=function(event){
??????//?執行腳本
???event=event||window.event;
???if(event.stopPropagation){
????event.stopPropagation();
???}else{
????event.cancelBubble=true;
???}
???menu.style.display='block';
????}?
為什么這里需要阻止事件冒泡呢,父元素也沒有類似的點擊事件?。?br />
2016-03-18
因為這里的選項,他的父是ul為祖父是id為divselect的div...,父和祖父都沒有添加鼠標點擊事件,的確事件冒泡了,但是他們都沒有事件處理程序,所以不需要阻止時間冒泡;
這里不需要了。因為按下按鍵后循序執行的關系,清除背景會先被執行。