點擊按鈕以開啟下拉式選單
<p>我正在使用 bootstrap 5 cdn,我想打開下拉列表而不單擊下拉列表。 </p>
<p>我使用導覽列並希望顯示「下拉連結」:</p>
<pre class="brush:php;toolbar:false;"><nav class="navbar navbar-expand-sm navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button
class="navbar-toggler"
type="button"
data-bs-toggle="collapse"
data-bs-target="#navbarNavDropdown"
aria-controls="navbarNavDropdown"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item dropdown">
<a
class="nav-link dropdown-toggle"
href="#"
id="test"
role="button"
data-bs-toggle="dropdown"
aria-expanded="false"
>
Dropdown link
</a>
<ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<button class="btn btn-primary btn-sm" (click)="open()">Abrir</button></pre>
<p>在組件中我嘗試執行以下操作,但它不起作用:</p>
<pre class="brush:php;toolbar:false;">export class AppComponent {
open() {
const dropdown = document.getElementById('test');
dropdown.click();
}
}</pre>
<p>我在 stackblitz 中做到了:測試</p>