简体   繁体   English

单击时更改链接图标

[英]Change link icon on click

I would like to change icon when the user clicks into the link by jQuery.当用户通过 jQuery 单击链接时,我想更改图标。 I've read some post from this page but I'm not able to get it.我从这个页面读过一些帖子,但我无法得到它。

This is my PHP template.这是我的 PHP 模板。

<!-- Information message -->
<?php if (!empty($message) || !empty($message_alert)): ?>
    <b><a class="expedition-info-link" href="#expedition-info" data-toggle="collapse">
      <?php print t('More info'); ?>
      <i class="fas fa-angle-right"></i>
    </a></b>
    <div id="expedition-info" class="collapse alert alert-info info">
      <?php if (!empty($message)): ?>
        <p><?php print nl2br($message); ?></p>
      <?php endif; ?>
      <?php if (!empty($alert_message)): ?>
        <p><?php print nl2br($alert_message); ?></p>
      <?php endif; ?>
    </div>
<?php endif; ?>

And this is the JS file.这是JS文件。

$('.expedition-info-link').on('click', function() {
    $(this).find('i')
      .toggleClass('fa-angle-right')
      .toggleClass('fa-angle-down');
});

I just wanna change the icon " fa-angle-right " for this one " fa-angle-down ".我只想为这个“ fa-angle-down ”更改图标“ fa-angle-right ”。 What am I doing wrong?我究竟做错了什么? Thanks all for help!感谢大家的帮助!

That could be achieved simply in one line using the jQuery method .toggleClass() .这可以使用 jQuery 方法.toggleClass()在一行中简单地实现。

 $('.expedition-info-link').on('click', function() { $('i', this).toggleClass('fa-angle-right fa-angle-down'); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link href="http://netdna.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet" /> <b> <a class="expedition-info-link" href="#expedition-info" data-toggle="collapse">More info <i class="fa fa-angle-right"></i></a> </b>

You can check the icon's class with .hasClass() and change it.您可以使用.hasClass()检查图标的类并更改它。

$('.expedition-info-link').on('click', function() {
    var icon =  $(this).find('i');
    if(icon.hasClass('fa-angle-right')){
         icon.removeClass('fa-angle-right');
         icon.addClass('fa-angle-down');
    }else{
         icon.addClass('fa-angle-right');
         icon.removeClass('fa-angle-down');
    }
  });

In Bootstrap 3 you should be able to use the .collapsed class too.在 Bootstrap 3 中,您也应该能够使用.collapsed类。 In that case no need for extra javascript logic to change your icons.在这种情况下,不需要额外的 javascript 逻辑来更改您的图标。

.expedition-info-link:before {
    content: '\f105'; // angle right
    font-family: FontAwesome;
}
.expedition-info-link.collapsed:before {
    content: '\f107'; // angle down
}

Additionally add aria-expanded="false" to initialize the state, whether they start collapsed or not.另外添加aria-expanded="false"来初始化状态,无论它们是否开始折叠。 Useful when using Bootstrap 3 & 4, you can now use [aria-expanded="false"] as a selector.在使用 Bootstrap 3 和 4 时很有用,您现在可以使用[aria-expanded="false"]作为选择器。

<a class="expedition-info-link" href="#expedition-info" data-toggle="collapse" aria-expanded="false">

.expedition-info-link[aria-expanded="false"]:before {
    content: '\f105'; // angle right
    font-family: FontAwesome;
}
.expedition-info-link[aria-expanded="true"]:before {
    content: '\f107'; // angle down
}

info on aria-expanded: https://getbootstrap.com/docs/4.0/components/collapse/#accessibility关于 aria-expanded 的信息: https : //getbootstrap.com/docs/4.0/components/collapse/#accessibility

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

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