js選項卡功能

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js選項卡功能</title>
    <style>
        .btn {
            width: 60px;
            hieght: 30px;
        }
 
        .content {
            width: 200px;
            height: 200px;
            border: 1px solid #b1b3b0;
            display: none;
        }
 
        .on {
            background: #2d64b3;
            color: #fff;
        }
 
    </style>
</head>
<body>
<button class="btn on">導航1</button>
<button class="btn">導航2</button>
<button class="btn">導航3</button>
<div class="content" style="display: block;">內容1</div>
<div class="content">內容2</div>
<div class="content">內容3</div>
</body>
<script>
    var btn = document.getElementsByTagName('button');
    var content = document.getElementsByClassName('content');
    for (var i = 0; i < btn.length; i++) {
        (function (n) {
            btn[i].onclick = function () {
                for (var j = 0; j < btn.length; j++) {
                    btn[j].className = "";
                    content[j].style.display = 'none'
                }
                this.className = "on";
                content[n].style.display = 'block'
            }
        }(i))
    }
</script>
</html>
相關文章
相關標籤/搜索