【问题标题】:Displaying show/hide content with a button and an .active css class使用按钮和 .active css 类显示显示/隐藏内容
【发布时间】:2018-12-20 14:10:57
【问题描述】:

我正在尝试在 wordpress 网站上创建一个推荐部分,其中有一个“扩展”按钮来显示完整的推荐报价。我希望按钮中的文本在单击后更改为“折叠”。我还需要向 div 包装器添加一个类,以便在按钮处于活动状态时实现自定义 css 样式。我需要这个粘贴三遍。问题是它在第一次推荐之后就失败了。

我使用下面的代码进行了此操作,它重复了三次(用于三个不同的推荐),并且它适用于基本的 html 文档。但是当我通过粘贴代码在 wordpress 站点中实现它时,只有第一个推荐完全有效。另外两个确实显示/隐藏了我的内部 div 元素,但它们不会插入 .active 类或将按钮的文本更改为“折叠”

第二个推荐都给出了一个

“Uncaught TypeError: Cannot set property 'innerHTML' of null” 在控制台中。

因此,例如,这是我想要展示的三分之二的推荐。我必须更改它们的 ID 以避免 javascript 冲突。

function showhide() {
  var content = document.getElementById('hidden-content');
  var wrap = document.getElementById('testimonial-wrap');
  var btn = document.getElementById('button1');

  if (content.style.display === 'none') {
    content.style.display = 'block';
    wrap.style.background = 'grey';
    btn.innerHTML = 'COLLAPSE';
    wrap.classList.add('active');
  } else {
    content.style.display = 'none';
    wrap.style.background = 'white';
    btn.innerHTML = 'EXPAND';
    wrap.classList.remove('active');
  }
}

function showhide2() {
  var content2 = document.getElementById('hidden-content2');
  var wrap2 = document.getElementById('testimonial-wrap2');
  var btn2 = document.getElementById('button2');

  if (content2.style.display === 'none') {
    content2.style.display = 'block';
    wrap2.style.background = 'grey';
    btn2.innerHTML = 'COLLAPSE';
    wrap2.classList.add('active');
  } else {
    content2.style.display = 'none';
    wrap2.style.background = 'white';
    btn2.innerHTML = 'EXPAND';
    wrap2.classList.remove('active');
  }
}
<div id="testimonial-wrap" style="background-color: white;">
  <div id="testimonial">
    above testimonial content
    <div id="hidden-content" style="display: none;">
      <p>"hidden content”</p>
    </div>
    <button id="button1" onclick="showhide()">EXPAND</button>
  </div>
</div>


<div id="testimonial-wrap2" style="background-color: white;">
  <div id="testimonial">
    above testimonial content
    <div id="hidden-content2" style="display: none;">
      <p>"hidden content.”</p>
    </div>
    <button id="button2" onclick="showhide2()">EXPAND</button>
  </div>
</div>

【问题讨论】:

  • 我编辑了您的问题,将您的代码转换为可运行的 sn-p,它似乎运行良好。你能重现sn-p中的错误吗?
  • 奇怪的是,它适用于静态 html 文件。当我将其调整为活动的 wordpress 安装时,我遇到了问题......

标签: javascript html css


【解决方案1】:

我认为这就是您要寻找的。您可以使用 jQuery 和少量代码更轻松地完成此操作。

我没有使用display: none,因为我想将过渡添加到动作中。 (转换不适用于display: none

$(document).ready(function() {
  $(".toggle-button").on("click", function() {
    $(this).closest(".testimonial-wrap").toggleClass("active");
  });
});
.testimonial-wrap {
  background-color: #C1C1C1;
  padding: 5px;
  margin-bottom: 10px;
}

.testimonial-wrap.active {
  background-color: #0095FF
}

.hidden-content {
  height: 0px;
  visibility: hidden;
  transition: all 0.5s ease-out;
}

.active .hidden-content {
  height: 100px;
  visibility: visible;
  transition: all 0.5s ease-in;
  background-color: rgba(0, 0, 0, 0.5);
}

button {
  display: inline-block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="testimonial-wrap">
  <div id="testimonial">
    <p>above testimonial content</p>
    <div class="hidden-content">
      <p>"hidden content”</p>
    </div>
    <button id="button1" class="toggle-button">EXPAND</button>
  </div>
</div>


<div class="testimonial-wrap">
  <div id="testimonial">
    <p>above testimonial content</p>
    <div class="hidden-content">
      <p>"hidden content.”</p>
    </div>
    <button id="button2" class="toggle-button">EXPAND</button>
  </div>
</div>

【讨论】:

  • 这太棒了!我喜欢我可以使用相同的 div ID 标签多次复制 html,而且我不必添加额外的 javascript 和 css 行来支持它!
  • 跟进问题...我实际上需要定位“testimonial-wrap div”以便向其中添加“.active”类。我需要将样式设置为整个包装器的背景div 处于活动状态。在现实世界的示例中,它可能在包装器和最终隐藏文本之间有或多或少的 div。jquery 有没有办法将类添加到特定的 ID?或使用“ $(this).parent" 选项在 html 中向上查找并将 .active 类应用于它看到的第一个实例?我尝试添加它,但它没有用。-- $(this).parent().find (".testimonial-wrap").toggleClass("active");
  • 由于某种原因,引号的结尾为我显示“.”而不是结束引号符号。它在这里工作正常,所以我不太确定为什么:/
  • hi @K.Dustin 根据您的后续问题,您可以使用函数$(this).parent(".testimonial-wrap") 准确查找具有此类或 ID 的父级。
  • 我更新了解决方案以满足您的需求。我弄错了,我们必须使用$(this).closest(".testimonial-wrap") 而不是parent()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-11
  • 2019-01-20
  • 1970-01-01
  • 1970-01-01
  • 2020-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多