JavaScriptDOM对象练习 tab栏切换

html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>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: purple;
        }

        .bd li {
            height: 255px;
            background-color: purple;
            display: none;
        }

        .bd li.current {
            display: block;
        }
    style>
    <script src="common.js">script>
head>
<body>
<div class="box" id="box">
    <div class="hd">
        <span class="current">体育span>
        <span>娱乐span>
        <span>新闻span>
        <span>综合span>
    div>
    <div class="bd">
        <ul>
            <li class="current">我是体育模块li>
            <li>我是娱乐模块li>
            <li>我是新闻模块li>
            <li>我是综合模块li>
        ul>
    div>
div>
<script>
    var hd = document.getElementsByClassName("hd")[0];
    var bd = document.getElementsByClassName("bd")[0];
    var spans = hd.getElementsByTagName("span");
    var lis = bd.getElementsByTagName("li");
    for (var i = 0; i < spans.length; i++) {
        spans[i].index = i;
        spans[i].onclick = function () {
            for (var j = 0; j < spans.length; j++) {
                spans[j].removeAttribute("class");
                lis[j].removeAttribute("class");
            }
            this.className = "current";
            lis[this.index].className = "current";
        }
    }


script>


body>
html>

你可能感兴趣的:(前端)