Rumah > Artikel > hujung hadapan web > 实现简单标签点击切换功能的JS代码分享
本文主要和大家介绍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>
相关推荐:
Atas ialah kandungan terperinci 实现简单标签点击切换功能的JS代码分享. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!