jquery - 使用 CSS 显示/隐藏文本

标签 jquery html css

我希望能够在页面上显示全文,但只显示第一段,然后使用“阅读更多”按钮显示隐藏文本。我想只使用 CSS 而不是 jQuery 来做到这一点,但想不出最好的方法。有什么想法吗?

所以类似的东西:

<p class="lead">This is the intro text</p>
<p>Read more</p>
<p class="hide"> This is the rest of the text, which is hidden on page load</p>

然后 CSS 会完成剩下的工作,但我不知道该做什么!

最佳答案

对于“可逆”点击类型功能,这里是复选框替代方案。

CodePen Demo

HTML

<div>
  <p class="lead">This is the intro text</p>
  <label for="toggle-1">Read More</label>
  <input type="checkbox" id="toggle-1">
  <p class="hide"> This is the rest of the text, which is hidden on page load</p>
</div>

CSS(带有一些基本样式)

div {
  margin:10px;
  border:1px solid lightgrey;
  padding:10px;
}

.hide {
  display:none;
}

label {
  background:lightblue;
  padding:5px;
  border-radius:5px;
  box-shadow:2px 3px 4px grey;
  border:1px solid blue;
}

label:active {
  box-shadow:0px 1px 2px grey;
}

input[type=checkbox] {
  position: absolute;
  top: -9999px;
  left: -9999px;
  /* For mobile, it's typically better to position checkbox on top of clickable
  area and turn opacity to 0 instead. */
}

/* Toggled State */
input[type=checkbox]:checked ~ .hide {
   display:block;
}

关于jquery - 使用 CSS 显示/隐藏文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19357254/

相关文章:

jquery - 在呈现 JSF 页面时显示加载/启动屏幕?

jquery - 使用 jquery cookie 时水平动画隐藏的 div

javascript - jQuery动画向左滑动​​没有文字换行

html - 调整大小后如何保持字体大小相对于容器宽度 (CSS)?

javascript - 将两个带有 alpha 边框的 .png 图像与 CSS 混合

jquery - <body></body> 标签正在从 jQueryMobile 插入到我的文档的头部

javascript - 从 URL 加载 JSON 数据以导入 HTML 表

html - 有没有办法让某些 Action 只在特定时间可用?

css - 当没有打开 Javascript 时。还有什么解决办法?

jquery - 尝试使用 jQuery 在 <div> 中水平分布不同大小的元素