試試這一個,但請注意,您必須通過使用兩個div
<script>
$(function()
{
function panel_show(curr_sec)
{
$("#" +curr_sec +" .panel-collapse").attr("aria-expanded","true");
$("#" +curr_sec +" .panel-title a").attr("aria-expanded","true");
$("#" +curr_sec +" .panel-collapse").addClass("in");
$("#" +curr_sec +" .panel-collapse").removeAttr("style")
$("#" +curr_sec +" .panel-title a").addClass("collapsed");
}
function panel_hide(curr_sec)
{
$("#" +curr_sec +" .panel-collapse").attr("aria-expanded","false");
$("#" +curr_sec +" .panel-title a").attr("aria-expanded","false");
$("#" +curr_sec +" .panel-collapse").removeClass("in");
$("#" +curr_sec +" .panel-collapse").removeAttr("style")
$("#" +curr_sec +" .panel-title a").removeClass("collapsed");
}
var bool="true";
var timesRun = 0;
var interval = setInterval(function(){
timesRun += 1;
$(".panel-collapse,.panel-title a").attr("aria-expanded","true");
$(".panel-collapse").addClass("in");
$(".panel-collapse").removeAttr("style")
$(".panel-title a").addClass("collapsed");
console.log(timesRun);
if(timesRun === 2){
$(".panel-collapse,.panel-title a").attr("aria-expanded","false");
$(".panel-collapse").removeClass("in");
$(".panel-collapse").removeAttr("style")
$(".panel-title a").removeClass("collapsed");
clearInterval(interval);
}
}, 1000);
$(".product-title h3").click(function()
{
var bool="true";
var curr_sec=' ';
var prev_sec=' ';
$(this).closest('div').parent('div').attr("id","Current_Section");
curr_sec = $(this).closest('div').parent('div').attr("id").toString();
$(this).closest('div').parent('div').siblings().attr("id","Prev_Section");
prev_sec=$(this).closest('div').parent('div').siblings().attr("id").toString();
$(this).toggleClass("Selected");
$(this).closest('div').parent('div').siblings().find("h3").removeClass("Selected")
if($(this).hasClass("Selected") === true)
{
panel_show(curr_sec);
panel_hide(prev_sec)
}
else
{
panel_hide(prev_sec)
panel_hide(curr_sec);
}
});
});
</script>
這裏來分隔的部分是你的HTML代碼:
<div id="section1">
<div class="product-title" style="width:100%;text-align:center;color:black;background-color:#3c4d5a;">
<h3 style="padding:5px;color:white;margin-bottom: 0;">Email</h3>
</div>
<div class="col-sm-6" style="background-color:#23ff7c;">In my Data Centre</div>
<div class="col-sm-6" style="background-color:#2691fa;">As a service</div>
<div class="col-sm-6">
<div class="panel-group" id="accordion">
<!-- First Panel -->
<div class="panel panel-default">
<div class="service-logo" style="float: left;"><img src="assets/scc.jpg" class="img-responsive" style="max-height:37px;margin-right: 10px;"></div>
<div class="panel-heading" style="border-radius: 0px;">
<h4 class="panel-title" data-toggle="collapse" data-target="#collapseOne">
<div class="service-title">Product or Service Title</div>
</h4>
<input type="checkbox" style="float:right" onclick="event.stopPropagation()" />
</div>
<div id="collapseOne" class="panel-collapse collapse">
<div class="panel-body">
<ul>
<li>Highlight One</li>
<li>Highlight Two</li>
<li>Highlight Three</li>
</ul>
<div class="service-description">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<div class="service-extras" style="">Datacentres located in: <b>France</b></div>
</div>
</div>
</div>
<!-- Second Panel -->
<div class="panel panel-default">
<div class="service-logo" style="float: left;"><img src="assets/scc.jpg" class="img-responsive" style="max-height:37px;margin-right: 10px;"></div>
<div class="panel-heading" style="border-radius: 0px;">
<h4 class="panel-title" data-toggle="collapse" data-target="#collapseTwo">
<div class="service-title">Product or Service Title</div>
</h4>
<input type="checkbox" style="float:right" onclick="event.stopPropagation()" />
</div>
<div id="collapseTwo" class="panel-collapse collapse">
<div class="panel-body">
<ul>
<li>Highlight One</li>
<li>Highlight Two</li>
<li>Highlight Three</li>
</ul>
<div class="service-description">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<div class="service-extras" style="">Datacentres located in: <b>France</b></div>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="panel-group" id="accordion">
<!-- First Panel -->
<div class="panel panel-default">
<div class="service-logo" style="float: left;"><img src="assets/scc.jpg" class="img-responsive" style="max-height:37px;margin-right: 10px;"></div>
<div class="panel-heading" style="border-radius: 0px;">
<h4 class="panel-title" data-toggle="collapse" data-target="#collapseFour">
<div class="service-title">Product or Service Title</div>
</h4>
<input type="checkbox" style="float:right" onclick="event.stopPropagation()" />
</div>
<div id="collapseFour" class="panel-collapse collapse">
<div class="panel-body">
<ul>
<li>Highlight One</li>
<li>Highlight Two</li>
<li>Highlight Three</li>
</ul>
<div class="service-description">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<div class="service-extras" style="">Datacentres located in: <b>France</b></div>
</div>
</div>
</div>
<!-- Second Panel -->
<div class="panel panel-default">
<div class="service-logo" style="float: left;"><img src="assets/scc.jpg" class="img-responsive" style="max-height:37px;margin-right: 10px;"></div>
<div class="panel-heading" style="border-radius: 0px;">
<h4 class="panel-title" data-toggle="collapse" data-target="#collapseFive">
<div class="service-title">Product or Service Title</div>
</h4>
<input type="checkbox" style="float:right" onclick="event.stopPropagation()" />
</div>
<div id="collapseFive" class="panel-collapse collapse">
<div class="panel-body">
<ul>
<li>Highlight One</li>
<li>Highlight Two</li>
<li>Highlight Three</li>
</ul>
<div class="service-description">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<div class="service-extras" style="">Datacentres located in: <b>France</b></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="section2">
<div class="product-title" style="width:100%;text-align:center;color:black;background-color:#3c4d5a;">
<h3 style="padding:5px;color:white;margin-bottom: 0;">another heading</h3>
</div>
<div class="col-sm-6" style="background-color:#23ff7c;">In my Data Centre</div>
<div class="col-sm-6" style="background-color:#2691fa;">As a service</div>
<div class="col-sm-6">
<div class="panel-group" id="accordion">
<!-- First Panel -->
<div class="panel panel-default">
<div class="service-logo" style="float: left;"><img src="assets/scc.jpg" class="img-responsive" style="max-height:37px;margin-right: 10px;"></div>
<div class="panel-heading" style="border-radius: 0px;">
<h4 class="panel-title" data-toggle="collapse" data-target="#collapseOne">
<div class="service-title">Product or Service Title</div>
</h4>
<input type="checkbox" style="float:right" onclick="event.stopPropagation()" />
</div>
<div id="collapseOne" class="panel-collapse collapse">
<div class="panel-body">
<ul>
<li>Highlight One</li>
<li>Highlight Two</li>
<li>Highlight Three</li>
</ul>
<div class="service-description">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<div class="service-extras" style="">Datacentres located in: <b>France</b></div>
</div>
</div>
</div>
<!-- Second Panel -->
<div class="panel panel-default">
<div class="service-logo" style="float: left;"><img src="assets/scc.jpg" class="img-responsive" style="max-height:37px;margin-right: 10px;"></div>
<div class="panel-heading" style="border-radius: 0px;">
<h4 class="panel-title" data-toggle="collapse" data-target="#collapseTwo">
<div class="service-title">Product or Service Title</div>
</h4>
<input type="checkbox" style="float:right" onclick="event.stopPropagation()" />
</div>
<div id="collapseTwo" class="panel-collapse collapse">
<div class="panel-body">
<ul>
<li>Highlight One</li>
<li>Highlight Two</li>
<li>Highlight Three</li>
</ul>
<div class="service-description">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<div class="service-extras" style="">Datacentres located in: <b>France</b></div>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="panel-group" id="accordion">
<!-- First Panel -->
<div class="panel panel-default">
<div class="service-logo" style="float: left;"><img src="assets/scc.jpg" class="img-responsive" style="max-height:37px;margin-right: 10px;"></div>
<div class="panel-heading" style="border-radius: 0px;">
<h4 class="panel-title" data-toggle="collapse" data-target="#collapseFour">
<div class="service-title">Product or Service Title</div>
</h4>
<input type="checkbox" style="float:right" onclick="event.stopPropagation()" />
</div>
<div id="collapseFour" class="panel-collapse collapse">
<div class="panel-body">
<ul>
<li>Highlight One</li>
<li>Highlight Two</li>
<li>Highlight Three</li>
</ul>
<div class="service-description">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<div class="service-extras" style="">Datacentres located in: <b>France</b></div>
</div>
</div>
</div>
<!-- Second Panel -->
<div class="panel panel-default">
<div class="service-logo" style="float: left;"><img src="assets/scc.jpg" class="img-responsive" style="max-height:37px;margin-right: 10px;"></div>
<div class="panel-heading" style="border-radius: 0px;">
<h4 class="panel-title" data-toggle="collapse" data-target="#collapseFive">
<div class="service-title">Product or Service Title</div>
</h4>
<input type="checkbox" style="float:right" onclick="event.stopPropagation()" />
</div>
<div id="collapseFive" class="panel-collapse collapse">
<div class="panel-body">
<ul>
<li>Highlight One</li>
<li>Highlight Two</li>
<li>Highlight Three</li>
</ul>
<div class="service-description">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<div class="service-extras" style="">Datacentres located in: <b>France</b></div>
</div>
</div>
</div>
</div>
</div>
</div>
小提琴是好的,但堆棧片段('<>'工具欄按鈕)使您的代碼可以在現場**運行**甚至更好。當你使用外部網站時,你可能會冒險將重要信息排除在問題之外(問題不能依賴於網站外的內容)。使用堆棧片段顯着提高了獲得有用答案的機會。 (如同格式化你的代碼,所以它不是大規模縮進,並且不在頁面的右側。) –
我已經將小提琴複製到了你的問題中,並完成了格式化。 (奇怪的是,片段編輯器中的「Tidy」按鈕確實不喜歡這些鏈接;可能值得檢查,您沒有未封閉標籤或兩個地方。) –
感謝您的幫助@ T.J.Crowder! – Henry