2016-02-14 23 views
3

jQuery爲三段文字,我想顯示一段文字點擊時會再次點擊時會隱藏。Javascript擴大段autoclose

下面是代碼我目前有:

$(document).ready(function() { 
$('.section').hide(); 
$('h3').click(function() { 
    $(this).toggleClass("open"); 
    $(this).next().toggle(); 
}); //end toggle 
}); 

比方說,你打開一個段落,但然後單擊另一個文本和擴大第2款,我要你打開,當你點擊展開自動關閉第一段另一個如果這是有道理的。我將如何繼續這樣做?

+1

爲所有段落分配一個類名稱 - 讓我們說「myClass」。在點擊函數中,首先隱藏所有的$(「。myClass」)。toggleClass(「close」)或用於摺疊段落的實際方法,然後在$(this)上執行切換以展開它。 – smozgur

+1

你能否提供一些html –

回答

0

以下引導手風琴你能否提供一些HTML,但如果我的理解以及

$(document).ready(function() { 
$('.section').hide(); 
$('h3').click(function() { 
    $(this).toggleClass("open"); 
    $(this).next().show(); //can use .slideDown() 
    $(this).next().siblings('.section').hide(); //can use .slideUp() 
}); //end toggle 
}); 

這將工作for:

<h3> heading section 1 </h3> 
<div class="section">Section1</div> 

<h3> heading section 2 </h3> 
<div class="section">Section1</div> 

<h3> heading section 3 </h3> 
<div class="section">Section1</div> 
+0

你好你的代碼工作得很好,只是想知道當你點擊擴展它的文本以及打開另一段時段落是否可能關閉。 –

+0

這裏是我的html http://pastebin.com/XHzwPDMP –

+0

@Krasimir Zarkov您可以設置單擊事件處理程序的.section僞 '$( '節')。點擊(函數(){$ (本) .hide(); });' –

0

這是手風琴的beahaviour - 簽出(http://www.tutorialrepublic.com/codelab.php?topic=bootstrap&file=accordion

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>Example of Bootstrap 3 Accordion</title> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
<style type="text/css"> 
    .bs-example{ 
     margin: 20px; 
    } 
</style> 
</head> 
<body> 
<div class="bs-example"> 
    <div class="panel-group" id="accordion"> 
     <div class="panel panel-default"> 
      <div class="panel-heading"> 
       <h4 class="panel-title"> 
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">1. What is HTML?</a> 
       </h4> 
      </div> 
      <div id="collapseOne" class="panel-collapse collapse in"> 
       <div class="panel-body"> 
        <p>HTML stands for HyperText Markup Language. HTML is the main markup language for describing the structure of Web pages. <a href="http://www.tutorialrepublic.com/html-tutorial/" target="_blank">Learn more.</a></p> 
       </div> 
      </div> 
     </div> 
     <div class="panel panel-default"> 
      <div class="panel-heading"> 
       <h4 class="panel-title"> 
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">2. What is Bootstrap?</a> 
       </h4> 
      </div> 
      <div id="collapseTwo" class="panel-collapse collapse"> 
       <div class="panel-body"> 
        <p>Bootstrap is a powerful front-end framework for faster and easier web development. It is a collection of CSS and HTML conventions. <a href="http://www.tutorialrepublic.com/twitter-bootstrap-tutorial/" target="_blank">Learn more.</a></p> 
       </div> 
      </div> 
     </div> 
     <div class="panel panel-default"> 
      <div class="panel-heading"> 
       <h4 class="panel-title"> 
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree">3. What is CSS?</a> 
       </h4> 
      </div> 
      <div id="collapseThree" class="panel-collapse collapse"> 
       <div class="panel-body"> 
        <p>CSS stands for Cascading Style Sheet. CSS allows you to specify various style properties for a given HTML element such as colors, backgrounds, fonts etc. <a href="http://www.tutorialrepublic.com/css-tutorial/" target="_blank">Learn more.</a></p> 
       </div> 
      </div> 
     </div> 
    </div> 
    <p><strong>Note:</strong> Click on the linked heading text to expand or collapse accordion panels.</p> 
</div> 
</body> 
</html>