本文主要和大家介紹JS實作的簡單標籤點擊切換功能,涉及javascript事件回應及頁面元素遍歷、屬性動態變換等相關操作技巧,需要的朋友可以參考下,希望能幫助到大家
具體程式碼如下:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>www.php.cn</title> <style> * { margin: 0; padding: 0; } ul { list-style-type: none; } .box { width: 400px; height: 300px; border: 1px solid #ccc; margin: 100px auto; overflow: hidden; } .hd { height: 45px; } .hd span { display: inline-block; width: 90px; background-color: pink; line-height: 45px; text-align: center; cursor: pointer; } .hd span.current { background-color: yellowgreen; } .bd li { height: 255px; background-color: yellowgreen; display: none; } .bd li.current { display: block; font-size: 36px; } </style> </head> <body> <p class="box" id="box"> <p class="hd"> <span class="current">体育</span> <span>娱乐</span> <span>新闻</span> <span>综合</span> </p> <p class="bd"> <ul> <li class="current">我是体育模块</li> <li>我是娱乐模块</li> <li>我是新闻模块</li> <li>我是综合模块</li> </ul> </p> </p> <script> var box = document.getElementById("box"); var spans = box.getElementsByTagName("span"); var lis = box.getElementsByTagName("li"); for (var i = 0; i < spans.length; i++) { spans[i].aaa = i; spans[i].onclick = function () { for (var i = 0; i < spans.length; i++) { spans[i].className = ""; lis[i].className = ""; } this.className = "current"; lis[this.aaa].className = "current"; } } </script> </body> </html>
相關建議:
JavaScript實作Tab點擊切換實例程式碼分享(圖文)
#以上是實作簡單標籤點擊切換功能的JS程式碼分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!