目的:制作选项卡
知识点:在循环中添加自己的属性index用于标识循环中的某个元素。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <style> #div1 input { float:left; } #div1 div { clear:both; width:100px; height:100px; background:gold; } .active { background:red; } </style> <script> window.onload = function() { var oDiv = document.getElementById(‘div1‘); var oInput = oDiv.getElementsByTagName(‘input‘); var oDivIn = oDiv.getElementsByTagName(‘div‘); for(var i=0; i<oInput.length; i++) { oInput[i].index = i; oInput[i].onclick = function() { mouseclick(this); } } function mouseclick(obj) { for(var i=0; i<oInput.length; i++) { oInput[i].className = ‘‘; oDivIn[i].style.display = ‘none‘; } obj.className = ‘active‘; oDivIn[obj.index].style.display = ‘block‘; } for(var i=1; i<oDivIn.length; i++) { oDivIn[i].style.display = ‘none‘; } } </script> </head> <body> <div id="div1"> <input type="button" value="教育"/> <input type="button" value="培训"/> <input type="button" value="招生"/> <input type="button" value="出国"/> <div>11</div> <div>22</div> <div>33</div> <div>44</div> </div> </body> </html>
时间: 2024-12-21 03:00:48