当我点击按钮时,没有任何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";
}
});
}
1条答案
按热度按时间x4shl7ld1#
您的代码可以正常工作,但它只会在第二次单击按钮后隐藏内容区域,因为初始值
dropdownContent.style.display
是空字符串,但您要检查它是否等于“block”。所以第一次点击按钮时将内容区域设置为
display: block
。您需要将检查更改为
dropdownContent.style.display === ""