javascript - 使用 jQuery 展开或折叠文本

标签 javascript jquery html css

我正在尝试学习如何使用 jQuery 来展开/折叠网页上的文本,但不确定如何使用“a href”链接来实现此目的?我希望用户通过单击“显示更多”链接来展开或折叠文本。我知道您必须添加或删除“隐藏”类才能执行此操作,但是我到底如何使用切换类来实现此目的?

* {
  margin: 0;
  padding: 0;
}
body {
  font-family: Verdana, Arial, Helvetica, sans-serif;
  font-size: 87.5%;
  width: 650px;
  margin: 0 auto;
  padding: 15px 25px;	
  border: 3px solid blue;
}
h1 { 
  font-size: 150%;
}
h2 {
  font-size: 120%;
  padding: .25em 0 .25em 25px;
}
div.hide {
  display: none;
}
ul {
  padding-left: 45px;
}
li {
  padding-bottom: .4em;
}
p, a {
  padding-bottom: .4em;
  padding-left: 25px;
}
a, a:focus, a:hover {
  color: blue;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" href="main.css">
  <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
  <script src="subset_expansion.js"></script>   	
</head>

<body>
  <main id="jdom">
    <h1>Expanding and Collapsing messages</h1>
    <h2>Message 1</h2>
    <div>
        <p>Click the link below to expand this message</p>
    </div>
    <div class="hide">
        <p>Hello!</p>
    </div>
    <a href="#">Show more</a>			

    <h2>Message 2</h2>
    <div>
        <p>Click the link below to expand this message</p>
    </div>
    <div class="hide">
      <p>BOOP</p>
    </div>
    <a href="#">Show more</a>

    <h2>Message 3</h2>
    <div>
        <p>Click the link below to expance this message</p>
    </div>
    <div class="hide"> 
      <p>Things to do when you're bored</p>
      <ul>
        <li>Go for a walk</li> 
        <li>Try your hand at painting.</li>
        <li>Listen to music</li>
      </ul>
    </div>
    <a href="#">Show more</a>	
  </main>
</body>
</html>

最佳答案

$(document).ready(function(){

  $('a').on( "click", function(){
    $(this).prev("div").toggleClass('hide');
    if($(this).text() == 'Show more')
    {
       $(this).text('Show Less');
    }
    else
    {
       $(this).text('Show more');
    }
  });
});
* {
	margin: 0;
	padding: 0;
}
body {
	font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 87.5%;
	width: 650px;
	margin: 0 auto;
	padding: 15px 25px;	
	border: 3px solid blue;
}
h1 { 
	font-size: 150%;
}
h2 {
	font-size: 120%;
	padding: .25em 0 .25em 25px;
}
div.hide {
	display: none;
}
ul {
	padding-left: 45px;
}
li {
	padding-bottom: .4em;
}
p, a {
	padding-bottom: .4em;
	padding-left: 25px;
}
a, a:focus, a:hover {
	color: blue;
}
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
    <title>Title</title>
	<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>   	
</head>

<body>
	<main id="jdom">
		<h1>Expanding and Collapsing messages</h1>
		<h2>Message 1</h2>
		<div>
            <p>Click the link below to expand this message</p>
        </div>
        <div class="hide">
            <p>Hello!</p>
		</div>
		<a href="javasctipt:void();">Show more</a>			
		
		<h2>Message 2</h2>
		<div>
            <p>Click the link below to expand this message</p>
        </div>
        <div class="hide">
			<p>BOOP</p>
		</div>
		<a href="javasctipt:void();">Show more</a>
			
		<h2>Message 3</h2>
		<div>
            <p>Click the link below to expance this message</p>
		</div>
		<div class="hide"> 
			<p>Things to do when you're bored</p>
			<ul>
				<li>Go for a walk</li> 
				<li>Try your hand at painting.</li>
				<li>Listen to music</li>
			</ul>
		</div>
		<a href="javasctipt:void();">Show more</a>				
	
	</main>
</body>
</html>

关于javascript - 使用 jQuery 展开或折叠文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43534363/

相关文章:

html - 不能相邻 float 两个 100vh 的 div

javascript - 我不确定我的 ajax 导航是否正常工作

jQuery .empty() && .append() 莫名其妙地失败了

javascript - 即使在使用 javascript 重新加载后也保留滚动条位置

javascript - 带有url的jQuery幻灯片在图像之间可变淡入淡出

javascript - 当图像在 holder 中显示时,将图像垂直居中显示

javascript - 如何正确制作HTML进度条

javascript - HTML linter 中的 Microsoft VS Code 和 Angular 2 标签

javascript - 使用 Angular 在 div 外部单击时关闭 div

javascript - Angular 指令 - 选择下拉数组或对象模型