简体   繁体   English

Bootstrap手风琴抑制器打开

[英]Bootstrap Accordion Suppress Open

I've got this jQuery, which is obviously not working: 我有这个jQuery,显然不起作用:

$('input.descriptionLC').click(function(){
    $('.collapse').collapse('hide')
});

My html is being generated by this for loop / append: 我的html是由此for循环/追加生成的:

for (i=0; i < nodeArr.length; i++){
  $('#accordion2').append('<div class="accordion-group">' +
  '<div class="accordion-heading">' +
    '<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapse' + i + '">' +  
        '<hr class="inLC">' +
        '<div class="divLC">' +
            '<p class="titleLC">' + nodeArr[i][0] + '</p>' +
            '<p class="imgWrap"><img class="imgLC" src="img/On_slider.png"></p>' +
            '<input type="text" class="descriptionLC" id="description' + i + '">' +
        '</div>' +    
    '</a>' +
  '</div>' +
  '<div id="collapse' + i + '" class="accordion-body collapse">' +
    '<div class="accordion-inner">' +
      'Anim pariatur cliche...' +
    '</div>' +
  '</div>' +
  '</div>'
  );
}

You want to use .on instead of .click as your DOM is changing. 您要在更改DOM时使用.on而不是.click。

See this answer . 看到这个答案 .on will (and should) be used to watch the parent of the changing area for changes; .on将(并且应该)用于监视更改区域的父项是否发生更改; once the changes have been executed it will automatically rebind all of the events to the selected subelements (with the filter). 一旦执行了更改,它将自动将所有事件重新绑定到选定的子元素(带有过滤器)。 In that answer I give a more detailed description of how to use it. 在该答案中,我将更详细地说明如何使用它。

In short, though this is inefficient (you should change body to some div containing just the changing part of the DOM): 简而言之,尽管这样做效率不高(您应该将body更改为仅包含DOM更改部分的div):

$('body').on("click", 'input.descriptionLC', function(e){
      $('.collapse').collapse('hide')
});

if you want just the following accordion item : 如果您只需要以下手风琴产品

$('body').on("click", 'input.descriptionLC', function(e){
      $(this).next('.collapse').collapse('hide')
});

Jquery jQuery的

$('body').on('click', 'input.descriptionLC', function(e){
    $('.collapse').collapse('hide');
});

See jQuery on docs 在文档上查看jQuery

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

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