0

有人可以幫助我解決小錯誤。我使用bootstrap 4 alpha 6.在下面你可以看到圖片。 enter image description here 正如你所看到的,我的頁面中有幾個摺疊塊。其中一個崩潰塊在其他崩潰塊內。當我點擊按鈕打開該內部塊(使用內部數據切換)時,它會更改頂部數據切換的圖標。我在JS中做了什麼錯誤?Bootstrap崩潰更改圖標

HTML:

<div class="card"> 
    <div class="card-header"> 
     <div class="d-flex align-items-center justify-content-between"> 
      <button data-toggle="collapse" data-target="#collapse-group-task" aria-expanded="false" aria-controls="collapse-group-task"> 
       <i class="fa fa-eye" aria-hidden="true"></i> 
      </button> 
     </div> 
    </div> 

    <div class="card-block"> 
     <div class="collapse" id="collapse-group-task"> 
     <div class="list-group"> 
      <div class="d-flex w-100 justify-content-end custom-d-flex"> 
       <a data-toggle="collapse" href="#collapse-group-task-1" aria-expanded="false" aria-controls="collapse-group-task-1"> 
        <i class="fa fa-comments-o" aria-hidden="true"></i>&#9;<span>Comments</span> 
       </a> 
      </div> 

      <div class="collapse w-100 comment-list-block" id="collapse-group-task-1"> 
        ***SOME TEXT*** 
      </div> 
     </div> 
     </div> 
    </div> 

</div> 
</div> 

JS:

$(document).ready(function() { 
    $('.collapse') 
     .on('shown.bs.collapse', function() { 
      $(this) 
       .parent().parent() 
       .find(".fa-eye") 
       .removeClass("fa-eye") 
       .addClass("fa-eye-slash"); 
     }) 
     .on('hidden.bs.collapse', function() { 
      $(this) 
       .parent().parent() 
       .find(".fa-eye-slash") 
       .removeClass("fa-eye-slash") 
       .addClass("fa-eye"); 
     }); 
}); 

回答

1

這是因爲你有內部手風琴手風琴,它們都具有相同的類崩,同event。解決辦法是用stopPropagation()停止parent element的事件。

在這裏,我做了一個小提琴:fiddle link

希望這會有所幫助!

1

您當前的jQuery將會聽取頁面上的所有摺疊事件,並且當您在accordion中添加accordion時,它們都具有相同的事件。您可以使用其他答案中提到的stopPropagation(),或者您可以檢查event.target,或者您可以向您的父級手風琴添加一個獨特的類並添加使用該類的事件偵聽器,這些都將解決您的問題。

這是一個解決方案,使用event.target僅在父手風琴打開或關閉時進行更改。

$(document).ready(function() { 
 
    $('.collapse') 
 
     .on('shown.bs.collapse', function(e) { 
 
\t \t if(e.target.id !== 'collapse-group-task'){return} 
 
      $(this) 
 
       .parent().parent() 
 
       .find(".fa-eye") 
 
       .removeClass("fa-eye") 
 
       .addClass("fa-eye-slash"); 
 
     }) 
 
     .on('hidden.bs.collapse', function(e) { 
 
\t \t if(e.target.id !== 'collapse-group-task'){return} 
 
      $(this) 
 
       .parent().parent() 
 
       .find(".fa-eye-slash") 
 
       .removeClass("fa-eye-slash") 
 
       .addClass("fa-eye"); 
 
     }); 
 
});
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width"> 
 
    <title>JS Bin</title> 
 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous"> 
 
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> \t 
 
</head> 
 
<body> 
 

 
<section class="container"> 
 
\t <section class="row"> 
 
<div class="card"> 
 
    <div class="card-header"> 
 
     <div class="d-flex align-items-center justify-content-between"> 
 
      <button data-toggle="collapse" data-target="#collapse-group-task" aria-expanded="false" aria-controls="collapse-group-task"> 
 
       <i class="fa fa-eye" aria-hidden="true"></i> 
 
      </button> 
 
     </div> 
 
    </div> 
 

 
    <div class="card-block"> 
 
     <div class="collapse" id="collapse-group-task"> 
 
     <div class="list-group"> 
 
      <div class="d-flex w-100 justify-content-end custom-d-flex"> 
 
       <a data-toggle="collapse" href="#collapse-group-task-1" aria-expanded="false" aria-controls="collapse-group-task-1"> 
 
        <i class="fa fa-comments-o" aria-hidden="true"></i>&#9;<span>Comments</span> 
 
       </a> 
 
      </div> 
 

 
      <div class="collapse w-100 comment-list-block" id="collapse-group-task-1"> 
 
        ***SOME TEXT*** 
 
      </div> 
 
     </div> 
 
     </div> 
 
    </div> 
 

 
</div> \t \t 
 
\t </section> 
 
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script> \t \t 
 
</body> 
 
</html>

上面的片段的Jsbin https://jsbin.com/libege/edit?html,js,output