简体   繁体   English

为什么我的可折叠按钮不折叠?

[英]Why my collapsible button doesn’t collapse?

I'd like to add to my (localhost) WordPress site a collapsible button with the following features:我想向我的(本地主机)WordPress 站点添加一个具有以下功能的可折叠按钮:

  • button width has not to be 100% but has to adapt to the text inside the button按钮宽度不必为 100%,但必须适应按钮内的文本
  • button can be inserted in a line of text without forcing the words after the button to go to the next line (I tried the plugin collapse-o-matic but the words after the button, even if they are written in the same line, are forced to go to the next line)按钮可以插入一行文本,而不会强制按钮后的文字转到下一行(我尝试了插件collapse-o-matic 但是按钮后的文字,即使它们写在同一行中,也是被迫转到下一行)

To better understand you can see this image为了更好地理解你可以看到这张图片

在此处输入图片说明

I was looking for how to manually (without plugins) create the button, and I ended up to a w3schools page where, using the online editor, I created the button I want (image above) whose code is the following我一直在寻找如何手动(没有插件)创建按钮,最后我找到了一个 w3schools 页面,在那里,我使用在线编辑器创建了我想要的按钮(上图),其代码如下

 var coll = document.getElementsByClassName("collapsible"); var i; for (i = 0; i < coll.length; i++) { coll[i].addEventListener("click", function() { this.classList.toggle("active"); var content = this.nextElementSibling; if (content.style.maxHeight) { content.style.maxHeight = null; } else { content.style.maxHeight = content.scrollHeight + "px"; } }); }
 .collapsible { background-color: #777; color: white; cursor: pointer; border: none; } .ccontent { padding: 0 18px; max-height: 0; overflow: hidden; transition: .3s ease-out; background-color: #f1f1f1; box-shadow: 5px 5px 10px 3px inset rgba(0, 0, 0, 0.60); }
 Does <button class="collapsible">this</button> work? <div class="ccontent"> <p>Yes!</p> </div> Good job!

Next, I tried to figure out how to add the code to WordPress, so this is what I did接下来,我试图弄清楚如何将代码添加到WordPress,所以这就是我所做的

  • added .collapsible and .content to the style.css file在 style.css 文件中添加了 .collapsible 和 .content
  • added the javascript to the footer using a plugin使用插件将 javascript 添加到页脚

then in a post I wrote the following code然后在帖子中我写了以下代码

Does <button class="collapsible">this</button> work?
<div class="ccontent"><p>Yes!</p></div>
Good job!

but, while the button is correctly showing, when clicking on it nothing happens.但是,虽然按钮正确显示,但单击它时什么也没有发生。 Since the button is showing, I think the problem has to do with the javascript code.由于按钮正在显示,我认为问题与 javascript 代码有关。 This is what is shown in my post这是我的帖子中显示的内容

在此处输入图片说明

as you can see another problem is the blank space between the first line and the second.如您所见,另一个问题是第一行和第二行之间的空白。

I'm almost sure that the javascript is loaded because when I open the page source (CTRL+U) I see that, some line before the </body> tag, the script is shown.我几乎可以肯定 javascript 已加载,因为当我打开页面源代码 (CTRL+U) 时,我看到在 </body> 标记之前的某些行显示了脚本。

As shown here Uncaught TypeError: Cannot read property 'style' of null at HTMLButtonElement.<anonymous> to let the buttons work is enough to edit如此处所示Uncaught TypeError: Cannot read property 'style' of null at HTMLButtonElement.<anonymous>让按钮工作足以编辑

var content = this.nextElementSibling;

in

var content = this.parentElement.nextElementSibling;


To remove the blank space is enough to place margin: -20px 0;去除空白空间足以放置margin: -20px 0; inside the .ccontent CSS..ccontent CSS 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM