大家重点看一下JS语句。报错是Uncaught TypeError: Cannot read property 'style' of undefined。
但是我要是用onclick来触发确是可以得,好奇怪啊!
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> *{margin:0; padding:0; font-size:13px; list-style:none;} .menu{width:210px; margin:50px auto; border:1px solid #ccc;} .menu p{height:25px; line-height:25px; font-weight:bold; background:#eee; border-bottom:1px solid #ccc; cursor:pointer; padding-left:5px;} .menu div ul{display:none;} .menu li{height:24px; line-height:24px; padding-left:5px;} </style> <script type="text/javascript"> window.onload=function(){ var menu = document.getElementById('menu'), ps =menu.getElementsByTagName('p'), uls = menu.getElementsByTagName('ul'); // 将所有点击的标题和要显示隐藏的列表取出来 for(i=0;i<ps.length;i++){ var u=uls[this.id]; ps[i].onmouseover= function(){ if (u.style.display == 'block') { u.style.display='none'; } } ps[i].onmouseout= function() { if (u.style.display == 'none') { u.style.display='block'; } } } } </script> </head> <body> <div id="menu"> <div> <p id="0">Web前端</p> <ul style="display:block"> <li>JavaScript</li> <li>DIV+CSS</li> <li>jQuery</li> </ul> </div> <div> <p id="1">后台脚本</p> <ul> <li>PHP</li> <li>ASP.net</li> <li>JSP</li> </ul> </div> <div> <p id="2">前端框架</p> <ul> <li>Extjs</li> <li>Esspress</li> <li>YUI</li> </ul> </div> </div> </body> </html>