[英]Pure Javascript-Generated Bootstrap4 Collapsible Won't Expand or Collapse
So, I'm very new at Javascript. 因此,我是Java语言的新手。 I'm trying to generate a Bootstrap 4 collapsible component using pure javascript.
我正在尝试使用纯JavaScript生成Bootstrap 4可折叠组件。 I can build a statically generated Bootstrap 4 collapsible all day long that works just fine.
我可以整天构建一个静态生成的可折叠Bootstrap 4,效果很好。 The problem comes in when I try to generate one with pure javascript in a dynamic fashion (createElement, setAttribute, appendChild, and so forth).
当我尝试以动态方式(createElement,setAttribute,appendChild等)用纯JavaScript生成代码时,就会出现问题。
When I try and create one this way, it simply doesn't work. 当我尝试以这种方式创建一个时,它根本不起作用。 I can click on it and it won't expand or collapse but I can issue commands like the following in the developer's console and watch it work just fine (yes, I realize the command below is jQuery, just trying to figure out what's going on):
我可以单击它,它不会展开或折叠,但是我可以在开发人员控制台中发出类似以下的命令,并观看它的工作正常(是的,我意识到下面的命令是jQuery,只是试图弄清楚发生了什么):
$('.collapse').collapse('toggle')
Furthermore, when I look at the elements of the page I created dynamically in the developer's console, they match the working example that I created statically. 此外,当我查看在开发人员控制台中动态创建的页面元素时,它们与我静态创建的工作示例匹配。
I've looked across the web for a few hours and tried several different iterations of code (using .class.add() instead of setAttribute() for example) and not run across any solution. 我在网上浏览了几个小时,并尝试了几次不同的代码迭代(例如,使用.class.add()而不是setAttribute()),并且没有在任何解决方案上运行。
No, no I don't completely know what I'm doing but I'm trying to learn. 不,不,我不完全知道自己在做什么,但我正在尝试学习。 I have some example code that illustrates the problem I'm having out on jsfiddle.net:
我有一些示例代码来说明我在jsfiddle.net上遇到的问题:
Bootstrap 4 Collapsible Experiment Bootstrap 4可折叠实验
If anyone can give me some pointers, I'd be very appreciative. 如果有人可以给我一些指导,我将不胜感激。 Thanks.
谢谢。
There is a typo in your code. 您的代码中有一个错字。 Your target and the id of your collapsed element are not the same.
您的目标和合拢元素的ID不相同。
What I did to track the error (maybe will this help you for the next time): I put the example of Bootstrap's site besides your code, and it did work. 我所做的跟踪错误的操作(可能对下次有帮助):我在代码之外放了Bootstrap网站的示例,它确实起作用。 So I knew Bootstrap was not the origin of the issue.
所以我知道Bootstrap并不是问题的根源。
Then I inspected (within the browser's inspector) both elements to see where they differ. 然后,我在浏览器的检查器中检查了这两个元素,以了解它们的不同之处。
When I saw a dash ( -
) in your id, I found it weird, because you will avoid using dashes in Javascript: Javascript and CSS, using dashes . 当您在ID中看到破折号(
-
)时,我发现它很奇怪,因为您将避免在Javascript中使用破折号: Javascript和CSS,使用破折号 。 So, I discovered the differences between your trigger and your target. 因此,我发现了触发器和目标之间的差异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.