iT邦幫忙

0

dropdown-btn-head 點選問題

我要點選 dropdown-btn-head 時顯示 id 為 tab2 的內容
但是我有在 dropdown-btn-head 標籤加上 data-target="#tab2" 測試時不成功,是我哪邊沒有寫道還是用法錯誤,請各位大大協助幫小女子看看~謝謝
底下程式會顯示一個 sidenav

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SideBarTEST.aspx.cs" Inherits="WebApplication_WEBTEST.SideBarTEST" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" />
    <title></title>
    <style>
        /* Fixed sidenav, full height */
        .sidenav {
            height: 100%;
            width: 200px;
            position: fixed;
            z-index: 1;
            top: 0%;
            left: 0px;
            background-color: #111;
            overflow-x: hidden;
            padding-top: 0px;
            overflow-y: scroll;
        }

            .sidenav::-webkit-scrollbar {
                display: none;
            }

        /* Hide scrollbar for IE and Edge */
        .sidenav {
            -ms-overflow-style: none;
        }

            /* Style the sidenav links and the dropdown button */
            .sidenav a, .dropdown-btn {
                padding: 6px 8px 6px 16px;
                text-decoration: none;
                font-size: 20px;
                color: #818181;
                display: block;
                border: none;
                background: none;
                width: 100%;
                text-align: left;
                cursor: pointer;
                outline: none;
            }

                /* On mouse-over */
                .sidenav a:hover, .dropdown-btn:hover {
                    color: #f1f1f1;
                }

        /* Add an active class to the active dropdown button */
        .active {
            /*background-color: green;*/
            color: white;
        }

        /* Dropdown container (hidden by default). Optional: add a lighter background color and some left padding to change the design of the dropdown content */
        .dropdown-container {
            display: none;
            background-color: #262626;
            padding-left: 8px;
        }

        /* Optional: Style the caret down icon */
        .fa-caret-down {
            float: right;
            padding-right: 8px;
        }

        /* Some media queries for responsiveness */
        @media screen and (max-height: 450px) {
            .sidenav {
                padding-top: 15px;
            }

                .sidenav a {
                    font-size: 18px;
                }
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <div class="col-3">
                <div class="sidenav nav-pills flex-column">
                    <button class="dropdown-btn" data-toggle="pill" data-target="#tab2">
                        <p style="font-size: medium">dropdown-btn-head</p>
                        <i class="fa fa-caret-down"></i>
                    </button>
                    <div class="dropdown-container">
                        <a data-toggle="modal" style="color: white">
                            <p style="font-size: medium">dropdown-item-A</p>
                        </a>
                        <a data-toggle="modal" style="color: white">
                            <p style="font-size: medium">dropdown-item-B</p>
                        </a>
                        <a data-toggle="modal" style="color: white">
                            <p style="font-size: medium">dropdown-item-C</p>
                        </a>
                    </div>
                </div>
            </div>
            <div class="col-9">
                <div class="tab-content" id="myTabContent">
                    <div class="tab-pane fade show active" id="tab1">
                        <div class="row">
                            <div class="card" style="width: 18rem;">
                                <div class="card-body">
                                    <h5 class="card-title">Card title_tab1</h5>
                                    <h6 class="card-subtitle mb-2 text-muted">Card subtitle_tab1</h6>
                                    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
                                    <a href="#" class="card-link">Card link</a>
                                    <a href="#" class="card-link">Another link</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="tab-pane fade" id="tab2">
                        <div class="row">
                            <div class="card" style="width: 18rem;">
                                <div class="card-body">
                                    <h5 class="card-title">Card title_tab2</h5>
                                    <h6 class="card-subtitle mb-2 text-muted">Card subtitle_tab2</h6>
                                    <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
                                    <a href="#" class="card-link">Card link</a>
                                    <a href="#" class="card-link">Another link</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!--bootstrap-->
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
    <script>
        /* Loop through all dropdown buttons to toggle between hiding and showing its dropdown content - This allows the user to have multiple dropdowns without any conflict */
        var dropdown = document.getElementsByClassName("dropdown-btn");
        var i;

        for (i = 0; i < dropdown.length; i++) {
            dropdown[i].addEventListener("click", function () {
                this.classList.toggle("active");
                var dropdownContent = this.nextElementSibling;
                if (dropdownContent.style.display === "block") {
                    dropdownContent.style.display = "none";
                } else {
                    dropdownContent.style.display = "block";
                }
            });
        }
    </script>
</body>
</html>

1 個回答

0
firecold
iT邦新手 3 級 ‧ 2020-01-30 18:02:03

我看你要的功能是這個
https://www.w3schools.com/Bootstrap/tryit.asp?filename=trybs_tabs_dynamic&stacked=h

但你的html寫的非常奇怪
你要用Dropdowns 但是demo卻沒看到Dropdowns的特性

依照需求簡單改法如下
https://codepen.io/firecold/pen/rNaggGo?editors=1111
在a上加入tab id
然後寫簡單jquery

Winnie iT邦新手 5 級 ‧ 2020-02-04 16:37:23 檢舉

我有參考下面的網址,已經做好了,謝謝你的幫忙~
http://www.hcg.com.tw/

我要發表回答

立即登入回答