打开最后一个动态添加的手风琴 [英] Making last dynamic added accordion open

查看:115
本文介绍了打开最后一个动态添加的手风琴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式添加引导手风琴,以便每次我单击添加"按钮,都会生成一个新的手风琴,并且上一个或任何其他关闭.我无法找出有关如何使用方法以及如何使用哪些选项的引导文档通过,我只放了一点我的东西,但这没用.

I am trying to add bootstrap accordion programmatically so that every time I click ADD button, a new accordion get generated and the previous or any other closes. I cannot figure out bootstrap docs on how to use methods and what options to pass, I have placed few of mine but it doesn't work.

if(this.aCount === 1){
   $('.collapse').collapse('hide');
 }else if(this.aCount === 2){
   $('.collapse').collapse('hide');
   $('.collapse').collapse('show');
 }

问题是我在手风琴上的id是动态的,所以我不能使用它,但是我的脚本总共会生成3个手风琴,并且每次添加一个新手风琴时,我想打开最近添加的一个手风琴并关闭所有打开的手风琴.现在,它打开了第一个.

The problem is my id on accordion are dynamic, so I cannot use that but in total my script generate 3 accordion and on every time it add a new accordion, I want to open recently added one and close any open ones. Right now It opens the very first one.

推荐答案

您可以使用.on方法在jQuery中委托事件",以便将事件绑定到动态创建的元素:

You can "Delegate Events" in jQuery using the .on method in order to bind events to dynamically created elements:

jQuery(document).on('click', '.collapse', function (e)
{
    // SOME .collapse WAS CLICKED!
    jQuery('.collapse').collapse('hide');
});

这样,每次单击.collapse元素时-即使该元素是动态创建的-其他所有元素都将关闭.

This way, every time a .collapse element is clicked - even if the element was created dynamically - all the others will close.

这篇关于打开最后一个动态添加的手风琴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆