This article mainly introduces the simple label click switching function implemented by JS, involving javascript event response, page element traversal, attribute dynamic transformation and other related operation skills. Friends in need can refer to it, I hope it can help everyone
The specific code is as follows:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>m.sbmmt.com</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>
Related recommendations:
jquery implementation of image enlargement and click switching function example sharing
JavaScript method to implement multiple overlapping layer click switching effects
JavaScript to implement Tab click switching example code sharing (picture and text)
The above is the detailed content of JS code sharing to implement simple label click switching function. For more information, please follow other related articles on the PHP Chinese website!