亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

鍵盤事件怎么處理

鍵盤事件怎么處理

清風流光 2017-05-05 13:51:04
<!DOCTYPE?html> <html> <head> <meta?charset="UTF-8"> <title>切換菜單</title> <link?rel="stylesheet"?type="text/css"?href="11.css"/> <script?src="11.js"?type="text/javascript"?charset="utf-8"></script> </head> <body> <div> <p?id='pid'>請選擇分類</p> <span?class="img"?id="img"></span> <ul?class="dis"?id="ul"> <li>ASP開發</li> <li>.NET開發</li> <li>PHP開發</li> <li>Javascript開發</li> <li>Java特效</li> </ul> </div> </body> </html>*{ margin:?0; padding:?0; } div{ width:?300px; margin:?3em?auto; text-align:?center; } p{ border:?2px?solid?lightsteelblue; width:?300px; height:?1.2em; line-height:?1.2em; } ul{ list-style:?none; border:?2px?solid?lightsteelblue; border-top:?none; width:?300px; height:?6em; line-height:?1.2em; } .dis{ display:?none; } .di{ display:?block; } .img{ display:?block; border:?2px?solid?lightsteelblue; height:1.2em; width:?1em; position:?absolute; top:?3em; background:?url(imgs/ptlogin.png)?no-repeat?top?-1em?left?5px; cursor:?pointer; }window.onload=function(){ aa() }; function?aa(){ var?span=document.getElementById('img'); var?ul=document.getElementById('ul'); span.onclick=open; document.onclick=clear; li=ul.childNodes; for(var?i=0;i<li.length;i++){ li[i].onmouseover=show; li[i].onmouseout=hide; li[i].onclick=replace; } document.onkeydown=show_1;? } function?open(event){ event.stopPropagation(); ul.className='di'; } function?clear(){ ul.className='dis'; } function?show(){ this.style.backgroundColor='gray'; } function?show_1(event){ if(event.keyCode===13){ clear(); } if(event.keyCode===40){ li[0].style.backgroundColor='gray'; } if(event.keyCode===38){ } } function?hide(){ this.style.backgroundColor='white'; } function?replace(){ pid=document.getElementById('pid'); pid.innerHTML=this.innerHTML; }測試按鍵的時候li[0]為什么都無法讀取有沒有一種好的方法可以更改this指向
查看完整描述

3 回答

已采納
?
zhangyudemuke

TA貢獻26條經驗 獲得超6個贊

由于編寫代碼的時候有換行產生空節點,如果確定其他地方沒有問題的話對代碼進行壓縮刪除空文本節點,這樣做的風險比較大,適合大神,麻煩一點的方法是對第一個節點進行驗證,

if(ul.childNodes[0].nodeType == 3 && ul.childNodes[0].nodeValue == ""){continue;}

將這個代碼放在for循環中試試看。

查看完整回答
1 反對 回復 2017-05-05
  • 清風流光
    清風流光
    鍵盤事件進行上下切換怎么消除之前的背景顏色
  • zhangyudemuke
    zhangyudemuke
    由于時間原因你的代碼我沒有仔細看,請諒解,就單單說如何更換背景顏色吧, function bgcolorToggle(color){ if(this.style.className == color){ this.style.className == "" }else{ this.className += " "+color } } 然后將這個更換背景的函數在click事件中調用,將click事件綁定在需要更改背景的元素上即可。
  • 清風流光
    清風流光
    冒泡事件是不是只針對點擊事件 ? onmouse事件沒有發生冒泡
點擊展開后面1
?
清風流光

TA貢獻8條經驗 獲得超0個贊

第一個應該是空節點的問題 this指向怎么更改?

查看完整回答
反對 回復 2017-05-05
  • 3 回答
  • 0 關注
  • 1536 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號