angularjs 在html中打开和关闭下拉列表

ukqbszuj  于 2022-10-31  发布在  Angular
关注(0)|答案(1)|浏览(154)

当我点击按钮时,没有任何React。我试图打开和关闭它们。我该如何解决这个问题?
HTML语言

<div class="sidenav">
    <img src="assets/images/placeholder-logo.svg">

    <li ng-repeat="item in data" ng-if="dataLoaded">
        <button class="dropdown-btn">{{item.streamName}}
            <i class="fa fa-caret-down"></i>
        </button>
        <div class="dropdown-container">
            <ul>
                <li ng-repeat="element in item.apps">
                    <button class="dropdown-btn">{{element.appName}}
                        <i class="fa fa-caret-down" ng-if="element.sheets.length"></i>
                    </button>
                    <ul>
                        <li ng-repeat="sheet in element.sheets">
                            <button class="dropdown-btn">{{sheet.sheetName}}
                            </button>
                        </li>
                    </ul>
                </li>
            </ul>
        </div>
    </li>
</div>

Java脚本

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";
                }
            });
            }
x4shl7ld

x4shl7ld1#

您的代码可以正常工作,但它只会在第二次单击按钮后隐藏内容区域,因为初始值dropdownContent.style.display是空字符串,但您要检查它是否等于“block”。
所以第一次点击按钮时将内容区域设置为display: block
您需要将检查更改为dropdownContent.style.display === ""

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 === "") {
            dropdownContent.style.display = "none";
        } else {
            dropdownContent.style.display = "block";
        }
    });
}

相关问题