我正在尝试使用accordian来创建我的页面。 单击按钮时它工作正常,但唯一的问题是div在页面加载时打开,我希望它关闭。它只应在单击按钮时打开。
<a href="" class="accordion">Click here</a>
<div class="panel" id="AccordionDiv">
<div class="store">
<div class="store-row">
<div class="cells store-logo text-center">
<img src="@strStaticWebsiteUrl@(objOfferPrice.StoreImage)" alt="" />
</div>
@if (objOfferPrice.Price < objOfferPrice.UrlPrice)
{
<div class="cells text-center">
<div class="product-price offer-price">Rs. @String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:0,0}", objOfferPrice.Price)<sup>*</sup></div>
<p class="real-price">Price: @objOfferPrice.UrlPrice</p>
</div>
}
</div>
</div>
这是html代码,下面是脚本代码。
<script>
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function () {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
});
}
</script>
答案 0 :(得分:2)
只需将style="display:none"
添加到您的pannel div
更改强>
<div class="panel" id="AccordionDiv">
要强>
<div class="panel" id="AccordionDiv" style="display:none">
答案 1 :(得分:0)
使用您拥有的代码,设置内联样式=&#34; display:none&#34;所以手风琴将默认关闭。推荐的方法是将类打开/关闭并在它们之间切换,而不是内联样式。
答案 2 :(得分:0)
最初隐藏它并在按钮点击时显示
<div class="panel" id="AccordionDiv" style="display:none";>
<script>
window.onload = function(){
var acc = document.getElementByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function () {
document.getElementById("AccordionDiv").style.display="block";
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
});
}}
</script>