【问题标题】:Show hide divs on click in HTML and CSS without jQuery在没有 jQuery 的情况下在 HTML 和 CSS 中单击时显示隐藏 div
【发布时间】:2013-10-10 20:03:11
【问题描述】:

我想要一些与位于此处的主题可折叠标题非常相似的东西:

http://jquerymobile.com/demos/1.2.0-alpha.1/docs/content/content-collapsible.html

不使用JQuery,这可能吗?

这是一个移动网站,但页面总是会离线,所以我真的不想使用 jquery。此外,为 jquery mobile 提供自定义样式比使用纯 css 并自己设置样式要困难得多。

【问题讨论】:

  • 页面离线不代表不能使用jquery,本地存储即可。
  • 没错,但我宁愿不使用它,也不愿缓存它。这是一个ios浏览器控件,所以我不需要支持ie 6/7。我还必须找到 jquery mobile 使用的所有图像并将它们存储在本地。

标签: javascript html css


【解决方案1】:

您可以使用checkbox模拟 onClickCSS

input[type=checkbox]:checked + p {
    display: none;
}

JSFiddle

Adjacent sibling selectors

【讨论】:

  • 为了改进这个答案,您还可以添加过渡效果:jsfiddle.net/37RGT
  • 您好,您知道此解决方案是否适用于电子邮件模板?
【解决方案2】:

使用labelcheckbox 输入

使选定的项目保持打开和可切换状态。

.collapse{
  cursor: pointer;
  display: block;
  background: #cdf;
}
.collapse + input{
  display: none; /* hide the checkboxes */
}
.collapse + input + div{
  display:none;
}
.collapse + input:checked + div{
  display:block;
}
<label class="collapse" for="_1">Collapse 1</label>
<input id="_1" type="checkbox"> 
<div>Content 1</div>

<label class="collapse" for="_2">Collapse 2</label>
<input id="_2" type="checkbox">
<div>Content 2</div>

使用label 并命名为radio 输入

类似于复选框,它只是关闭已经打开的复选框。
在两个输入上使用name="c1" type="radio"

.collapse{
  cursor: pointer;
  display: block;
  background: #cdf;
}
.collapse + input{
  display: none; /* hide the checkboxes */
}
.collapse + input + div{
  display:none;
}
.collapse + input:checked + div{
  display:block;
}
<label class="collapse" for="_1">Collapse 1</label>
<input id="_1" type="radio" name="c1"> 
<div>Content 1</div>

<label class="collapse" for="_2">Collapse 2</label>
<input id="_2" type="radio" name="c1">
<div>Content 2</div>

使用tabindex:focus

类似于radio 输入,另外您可以使用 Tab 键触发状态。
在手风琴外部单击将关闭所有打开的项目。

.collapse > a{
  background: #cdf;
  cursor: pointer;
  display: block;
}
.collapse:focus{
  outline: none;
}
.collapse > div{
  display: none;
}
.collapse:focus div{
  display: block; 
}
<div class="collapse" tabindex="1">
  <a>Collapse 1</a>
  <div>Content 1....</div>
</div>

<div class="collapse" tabindex="1">
  <a>Collapse 2</a>
  <div>Content 2....</div>
</div>

使用:target

类似于使用radio输入,可以额外使用Tab键进行操作

.collapse a{
  display: block;
  background: #cdf;
}
.collapse > div{
  display:none;
}
.collapse > div:target{
  display:block; 
}
<div class="collapse">
  <a href="#targ_1">Collapse 1</a>
  <div id="targ_1">Content 1....</div>
</div>

<div class="collapse">
  <a href="#targ_2">Collapse 2</a>
  <div id="targ_2">Content 2....</div>
</div>

使用&lt;detail&gt;&lt;summary&gt; 标签(纯HTML)

您可以使用 HTML5 的详细信息和摘要标签来解决此问题,而无需任何 CSS 样式或 Javascript。请注意,Internet Explorer 不支持这些标签。

<details>
  <summary>Collapse 1</summary>
  <p>Content 1...</p>
</details>
<details>
  <summary>Collapse 2</summary>
  <p>Content 2...</p>
</details>

【讨论】:

  • 不错的解决方案,是否可以一次展开多个并能够通过再次单击来折叠打开的一个?
  • @dev6546 当然,看看第一个演示链接
  • 感谢您提供详细信息/摘要 html5 解决方案。发现! :)
  • 这太棒了!有谁知道我是否可以在电子邮件模板上使用其中一种解决方案?
【解决方案3】:

当然! jQuery 毕竟只是一个使用 javascript 的库。

您可以使用 document.getElementById 获取相关元素,然后通过 element.style.height 相应地更改其高度。

elementToChange = document.getElementById('collapseableEl');
elementToChange.style.height = '100%';

将其封装在一个简洁的小函数中,该函数可满足来回切换的需求,您就有了自己的解决方案。

【讨论】:

    【解决方案4】:

    我喜欢 Roko 的回答,并在其中添加了几行,以便您获得一个三角形,该三角形在元素隐藏时指向右侧,在元素显示时指向下方:

    .collapse { font-weight: bold; display: inline-block; }
    .collapse + input:after { content: " \25b6"; display: inline-block; }
    .collapse + input:checked:after { content: " \25bc"; display: inline-block; }
    .collapse + input { display: inline-block; -webkit-appearance: none; -o-appearance:none; -moz-appearance:none;  }
    .collapse + input + * { display: none; }
    .collapse + input:checked + * { display: block; }
    

    【讨论】:

      猜你喜欢
      • 2012-08-05
      • 2011-01-15
      • 2023-03-15
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多