2017-01-19 78 views
1

我嘗試了幾種不同的jQuery方法,但無法使其工作。我只是尋找一個簡單的解決方案來切換2 fontawesome圖標。點擊時,我可以看到添加的新課程,但舊課程並未被刪除。與toggleClass問題,切換圖標不切換

$('.arrow-up').on('click', function(){ 
 
     $('.fa-chevron-down').toggleClass('fa-chevron-up'); 
 
     });
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous"> 
 
\t <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> 
 

 
<div class="container"> 
 
\t <div class="row"> 
 
\t \t <div class="col-md-10 offset-md-2 pt-3"> 
 
\t \t \t <div class="btn btn-success arrow-up"> 
 
\t \t \t \t <i class="fa fa-chevron-down"> 
 
       </i> 
 
\t \t \t \t Show this</div> 
 
\t \t </div> 
 
\t </div> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

任何幫助將不勝感激。

回答

2

通過使用空格將它們分開來切換兩個類。

$('.arrow-up').on('click', function() { 
 
    // select `i` tag with class `fa` withtin the div and toggle both classes 
 
    $('i.fa', this).toggleClass('fa-chevron-up fa-chevron-down'); 
 
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous"> 
 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> 
 

 
<div class="container"> 
 
    <div class="row"> 
 
    <div class="col-md-10 offset-md-2 pt-3"> 
 
     <div class="btn btn-success arrow-up"> 
 
     <i class="fa fa-chevron-down"> 
 
       </i> 
 
     Show this</div> 
 
    </div> 
 
    </div> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

+1

感謝Pranav,我實際上做了嘗試,但我引用了錯誤的選擇。無論如何,這確實有竅門! – Lucky500