[英]jquery Accordion - how to open the second tab by default on page loading
我是jquery和javascript的絕對菜鳥,而且我只是使用我在網上找到的簡單代碼。 這會創建一個手風琴列表,但我想在頁面加載時打開第二個選項卡(或第一個選項卡以外的任何選項卡)。 然后,在單擊任何其他選項卡時,應關閉默認打開的選項卡。
這是我正在使用的代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Document</title>
<style>
.accord-content { display: none; }
</style>
</head>
<body>
<div class="accordion">
<div class="accord-header">Header 1</div>
<div class="accord-content">This is the content for the first header.</div>
<div class="accord-header">Header 2</div>
<div class="accord-content">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
</div>
</body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".accordion .accord-header").click(function() {
if($(this).next("div").is(":visible")){
$(this).next("div").slideUp("slow");
} else {
$(".accordion .accord-content").slideUp("slow");
$(this).next("div").slideToggle("slow");
}
});
});
</script>
</html>
我怎樣才能做到這一點? 最簡單,更好。
謝謝大家提前。
您可以使用.trigger()模擬所需選項卡上的單擊。 這是一個工作小提琴 。
在你的jquery上,在document.ready結束之前:
$(".accordion .accord-header:eq(1)").trigger('click');
所以你的最終JS:
$(document).ready(function() {
$(".accordion .accord-header").click(function() {
if($(this).next("div").is(":visible")){
$(this).next("div").slideUp("slow");
} else {
$(".accordion .accord-content").slideUp("slow");
$(this).next("div").slideToggle("slow");
}
});
$(".accordion .accord-header:eq(1)").trigger('click');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.