简体   繁体   English

没有唯一标识符的自举崩溃

[英]Bootstrap collapse without unique identifier

I'm trying to use the bootstrap collapse plugin in such a way that a unique identifier is not required. 我正在尝试以不需要唯一标识符的方式使用bootstrap崩溃插件。 Normally there is usually a single or a couple of collapsible elements on a page. 通常,页面上通常只有一个或几个可折叠元素。

But my elements are generated dynamically and passing index keys is overkill. 但是我的元素是动态生成的,并且传递索引键是过大的。

What happens now is that if I toggle the collapse for element2 , it will collapse element1 . 现在发生的是,如果我切换element2的折叠,它将折叠element1 Obviously because they have the same ID. 显然是因为它们具有相同的ID。

Any way to achieve this without actually giving each collapsible element a unique id? 在没有实际为每个可折叠元素赋予唯一标识的情况下,有什么方法可以实现?

Here's a functional js fiddle: 这是一个实用的js小提琴:

http://jsfiddle.net/hhvrjnr3/ http://jsfiddle.net/hhvrjnr3/

It can be done. 可以办到。 First remove the data-target="#collapseExample" from the elements you want to collapse. 首先,从要折叠的元素中删除data-target="#collapseExample" Then add an extra class to your toggle button, I've added 'collapser'. 然后在您的切换按钮中添加一个额外的类,我添加了'collapser'。 That's not really needed, but it's nice to identify the toggle button. 确实不是必需的,但是很容易识别切换按钮。 Then add some jQuery to do the toggling, in this case I am using next() to get the subsequent element to the toggle button which is your element you wish to collapse. 然后添加一些jQuery进行切换,在这种情况下,我使用next()将后继元素添加到切换按钮,这是您希望折叠的元素。

$('.collapser').click(function() {
    $(this).next().collapse('toggle');
});

Example jsFiddle jsFiddle示例

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

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