Jquery 始终使用第一个元素id。如何使其重置事件并实际使用元素

Jquery 始终使用第一个元素id。如何使其重置事件并实际使用元素,jquery,bootstrap-4,accordion,Jquery,Bootstrap 4,Accordion,我正在使引导手风琴有改变图标时,它显示和隐藏它的改变图标回到以前。我做了这个函数,它几乎可以工作,但我的问题是,当我点击时,它总是取元素的第一个ID,而不是实际的元素ID。是否有可能获得元素的实际id,而不仅仅是第一个元素 jQuery(document).on('show.bs.collapse', function() { var childIdStr = jQuery(this).children().find('i').attr('id'); var myId = "#

我正在使引导手风琴有改变图标时,它显示和隐藏它的改变图标回到以前。我做了这个函数,它几乎可以工作,但我的问题是,当我点击时,它总是取元素的第一个ID,而不是实际的元素ID。是否有可能获得元素的实际id,而不仅仅是第一个元素

jQuery(document).on('show.bs.collapse', function() {

    var childIdStr = jQuery(this).children().find('i').attr('id');
    var myId = "#" + childIdStr;

    jQuery(myId).removeClass('dashicons-plus');
    jQuery(myId).addClass('dashicons-minus');
});

jQuery(document).on('hide.bs.collapse', function() {
    var childHideIdStr = jQuery(this).children().find('i').attr('id');
    var myId = "#" + childHideIdStr;

    if(jQuery('.collapse').hasClass('show')){
        jQuery(myId).removeClass('dashicons-minus');
        jQuery(myId).addClass('dashicons-plus');
    }    
});

居中
亚利克维吉1号
6240 Løgumkloster

电传 74928118(hverdage fra 8.00-15.00)

电子邮件 hviding。asylsyd@toender.dk

中心hviding 里贝韦
6720里贝

电传 74929359(hverdage fra 8.00-15.00)

电子邮件 hviding。asylsyd@toender.dk

将jQuery更改为

jQuery(document).on('show.bs.collapse', function(e) {
   var childIdStr = e.currentTarget.activeElement.firstChild.id;
   var myId = "#" + childIdStr;
   jQuery(myId).removeClass('dashicons-plus');
   jQuery(myId).addClass('dashicons-minus');
});

jQuery(document).on('hide.bs.collapse', function(e) {

   var childHideIdStr = e.currentTarget.activeElement.firstChild.id;
   var myId = "#" + childHideIdStr;

   if(jQuery('.collapse').hasClass('show')){
      jQuery(myId).removeClass('dashicons-minus');
      jQuery(myId).addClass('dashicons-plus');
   }
});
jQuery('a[data-toggle="collapse"] i').on('click',function(e){
   $(this).toggleClass('dashicons-plus');
   $(this).toggleClass('dashicons-minus');
});

只需将复杂的jQuery更改为

jQuery(document).on('show.bs.collapse', function(e) {
   var childIdStr = e.currentTarget.activeElement.firstChild.id;
   var myId = "#" + childIdStr;
   jQuery(myId).removeClass('dashicons-plus');
   jQuery(myId).addClass('dashicons-minus');
});

jQuery(document).on('hide.bs.collapse', function(e) {

   var childHideIdStr = e.currentTarget.activeElement.firstChild.id;
   var myId = "#" + childHideIdStr;

   if(jQuery('.collapse').hasClass('show')){
      jQuery(myId).removeClass('dashicons-minus');
      jQuery(myId).addClass('dashicons-plus');
   }
});
jQuery('a[data-toggle="collapse"] i').on('click',function(e){
   $(this).toggleClass('dashicons-plus');
   $(this).toggleClass('dashicons-minus');
});

您可以在单击事件或放置自定义属性进行选择时传递当前对象。