2016-02-14 33 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>