我有 4 個引導手風琴都倒塌了。從導航鏈接單擊時([href="#headingTwo"] 等...],我只需要展開參考的手風琴。[如果單擊另一個導航鏈接則關閉它]
所以下面的代碼有效,但我重復了 4 次......當然可以將它簡化為一個函式?
// accordion 1
$('a.js-scroll-trigger[href="#headingTwo"]').on('click', function() {
document.getElementById("collapseTwo").classList.add("show");
});
// accordion 2
$('a.js-scroll-trigger[href="#headingThree"]').on('click', function() {
document.getElementById("collapseThree").classList.add("show");
});
// accordion 3
$[...]
// accordion 4
$[...]
提前謝謝了。
uj5u.com熱心網友回復:
看起來您的錨點共享同一個類.js-scroll-trigger,您可以將點擊處理程式分配給所有它們,在處理程式中,您將從錨點獲取目標手風琴項href,它是id手風琴項的,然后您可以觸發click處理程式如下所示的專案按鈕。
href包含手風琴標題id,元素包含button觸發展開/折疊的第一個子元素
<h2 class="accordion-header" id="headingOne">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Accordion Item #1
</button>
</h2>
$(function() {
$("a.js-scroll-trigger").on("click", function() {
var target = $(this).attr("href");
$(target).find("button").trigger("click");
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.1.3/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<a href="#headingOne" class="js-scroll-trigger">Item 1</a>
<a href="#headingTwo" class="js-scroll-trigger">Item 2</a>
<a href="#headingThree" class="js-scroll-trigger">Item 3</a>
</div>
<br>
<div class="accordion" id="accordionExample">
<div class="accordion-item">
<h2 class="accordion-header" id="headingOne">
<button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Accordion Item #1
</button>
</h2>
<div id="collapseOne" class="accordion-collapse collapse show" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
<div class="accordion-body">
<strong>This is the first item's accordion body.</strong> It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and
hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="headingTwo">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Accordion Item #2
</button>
</h2>
<div id="collapseTwo" class="accordion-collapse collapse" aria-labelledby="headingTwo" data-bs-parent="#accordionExample">
<div class="accordion-body">
<strong>This is the second item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing
and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="headingThree">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
Accordion Item #3
</button>
</h2>
<div id="collapseThree" class="accordion-collapse collapse" aria-labelledby="headingThree" data-bs-parent="#accordionExample">
<div class="accordion-body">
<strong>This is the third item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and
hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
</div>
</div>
</div>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/484781.html
