如何通过单击图标更改 font-awesome 图标的颜色

2024-03-02

var garbage = document.getElementById("garbage");

garbage.addEventListener("click",function(){
  garbage.style.color = "#66c144";
}
<link href="https://use.fontawesome.com/releases/v5.0.10/css/all.css" rel="stylesheet">

<div id="garbage">
  <i class="fas fa-trash"></i>
</div>

您好,我正在尝试通过单击图标来更改字体很棒的垃圾图标的字体颜色。但是,它不起作用。我将不胜感激有关如何完成这项工作的任何提示。


var trash = document.getElementById("trash"),
    glass = document.getElementById("glass"),
    organic = document.getElementById("organic"),
    plastic = document.getElementById("plastic"),
    paper = document.getElementById("paper");

trash.addEventListener("click",function(){
    this.children[0].style.color = "#66c144";
    glass.children[0].style.color = "#ffffff";
    organic.children[0].style.color = "#ffffff";
    plastic.children[0].style.color = "#ffffff";
    paper.children[0].style.color = "#ffffff";
});

glass.addEventListener("click",function(){
    this.children[0].style.color = "#66c144";
    trash.children[0].style.color = "#ffffff";
    organic.children[0].style.color = "#ffffff";
    plastic.children[0].style.color = "#ffffff";
    paper.children[0].style.color = "#ffffff";
    
});

organic.addEventListener("click",function(){
    this.children[0].style.color = "#66c144";
    trash.children[0].style.color = "#ffffff";
    glass.children[0].style.color = "#ffffff";
    plastic.children[0].style.color = "#ffffff";
    paper.children[0].style.color = "#ffffff";
    
});

plastic.addEventListener("click",function(){
    this.children[0].style.color = "#66c144";
    trash.children[0].style.color = "#ffffff";
    glass.children[0].style.color = "#ffffff";
    organic.children[0].style.color = "#ffffff";
    paper.children[0].style.color = "#ffffff";
});

paper.addEventListener("click",function(){
    this.children[0].style.color = "#66c144";
    trash.children[0].style.color = "#ffffff";
    glass.children[0].style.color = "#ffffff";
    organic.children[0].style.color = "#ffffff";
    plastic.children[0].style.color = "#ffffff";
});
<link href="https://use.fontawesome.com/releases/v5.0.10/css/all.css" rel="stylesheet">

          <div class="icons flex">
                <div id="trash">
                    <i class="fas fa-trash"></i> 
                </div>    
                <div id="glass">
                    <i class="fas fa-wine-glass"></i>
                </div>
                <div id="organic">
                    <i class="fab fa-envira"></i>
                </div>
                <div id="plastic">
                    <i class="far fa-hdd"></i>
                </div>    
                <div id="paper">
                    <i class="far fa-newspaper"></i>
                </div>    
          </div>
UPDATE So Thanks to everyone's help, I managed to get the result I wanted (Every time I click a different icon, only the icon I click has the different color). On top of that, I am curious if there is a less repetitive or messy way to execute what I have now like using "Function" or something. I would appreciate if I can get some tips on it.

你错过了);收市后}:

var garbage = document.getElementById("garbage");

garbage.addEventListener("click",function(){
  garbage.style.color = "#66c144";
});
<link href="https://use.fontawesome.com/releases/v5.0.10/css/all.css" rel="stylesheet">

<div id="garbage">
  <i class="fas fa-trash"></i>
</div>

Note:你不需要target the parent,你可以只给id到图标。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何通过单击图标更改 font-awesome 图标的颜色 的相关文章

随机推荐

  • iAd 横幅在模拟器上显示测试广告,但在设备上不显示

    我正在尝试在我的 iPhone 应用程序上放置 iAd 横幅 这是我声明横幅广告的地方 void bannerViewDidLoadAd ADBannerView banner UIView beginAnimations nil cont
  • C++代码可视化

    一种后续 相关问题this https stackoverflow com questions 27857 c c source code visualization 我试图掌握一个拥有数百个类和一个大型继承层次结构的大型代码库 我希望能够
  • 为什么我无法从我的 GitLab 作业连接到 selenium docker-compose 服务?

    我正在 Gitlab CI 中运行 selenium 测试 但在使用 gitlab 运行程序而不是我的计算机时 无法正确设置远程 URL 跑步者的IP地址是192 168 xxx xxx 当我运行管道时 我得到的 selenium hub
  • Webstorm 无法识别 Grunt

    所以我搞砸了删除和安装节点和 npm 来安装没有 sudo 的软件包 现在我无法在 Webstorm 中使用 Grunt 面板 消息是 grunt no color gruntfile Users max repos cb Gruntfil
  • 将布尔值保存在 nsuserdefaults 中

    当我的应用程序启动时 正在播放音乐 void playBgMusic NSString path NSBundle mainBundle pathForResource bgmusic ofType aif theAudio AVAudio
  • 无法通过输入导入协议

    我在 Windows 10 上全新安装了 Python 3 7 32 我想尝试 Protocols python 方法 接下来我会这样做 文件 test protocols py 只有一行 from typing import Protoc
  • Asyncio 协程从未等待错误

    我无法解决和理解这里的问题 我正在使用一个示例来学习 Asyncio 但我使用的代码与我的代码类似 但我的代码给出了一条错误消息 sys 1 运行时警告 从未等待协程 run script 请提供任何帮助 我们将不胜感激 下面是我的代码 a
  • 区分大小写的连接 SQL

    我在这里有点停滞不前 我有一个简单的左外连接到一个返回 ID 的表 我的代码是 Select distinct TenantID Name Name2 TenantNumber Cashname From Tenants LEFT OUTE
  • 如何确定 nsstring 的哪一部分适合矩形?

    这是关于使用核心图形进行 PDF 格式化 但当涉及到在两页上打印字符串时 它可能涉及任何类型的分页 我需要将一个字符串分散到几页上 对于较小的字符串 这不是真正的问题 对于那些我使用 NSString UIKit 扩展的人sizeWithF
  • 在 stat 方法之前检查权限以避免错误

    我第一次尝试遍历目录 但是stat由于似乎缺乏权限 某些目录会抛出错误 Error EPERM operation not permitted stat K System Volume Information 我想避免打电话stat首先在给
  • Rxjs 基于条件的条件 switchMap

    我有这样的情况 myObservable1 pipe switchMap result1 MyObservable1 gt if condition return myObservable2 else return of null subs
  • 通过 PhpMyAdmin 复合外键约束?

    有没有办法通过 PhpMyAdmin 界面向 InnoDB 表添加复合 多列 外键 我已经在目标表中拥有适当的复合主键 并且可以使用单列外键约束 但我无法在界面中找到一种方法来执行复合主键 Table Log Date Service PR
  • 如何通过 API 了解谁在 Google 云端硬盘的共享云端硬盘(团队云端硬盘)中创建了该文件

    所有有权访问共享云端硬盘 团队云端硬盘 的用户都可以访问 Google 云端硬盘中的共享云端硬盘 团队云端硬盘 中的所有文件 默认情况下 在共享云端硬盘 团队云端硬盘 中创建新文件时 所有有权访问共享云端硬盘 团队云端硬盘 的用户也将成为所
  • 带有字符串键的 ASP.Net OData

    我正在尝试使用 ASP Net OData v4 例如 ODataController 来允许访问密钥为字符串的位置 95 的示例使用整数作为键 而我发现讨论使用字符串作为键的步骤的几篇文章对我来说不起作用 在所有情况下 我都尝试使用以下
  • 带零填充数字的 Matlab 文件名

    我有 11x11 矩阵 我将它们保存为 mat文件来自F01 01 to F11 11 我必须运行一个函数Func在每个文件上 由于需要很长时间 我想编写一个脚本来自动运行该函数 for i 01 11 for j 01 11 filena
  • 为属性设置默认值

    是否可以在没有属性主体的情况下设置默认值 最好有注释 SetTheDefaultValueTo true public bool IsTrue get set SetTheDefaultValueTo false public bool I
  • Swift3 sqlite3_open() 打开现有文件

    let file url try FileManager default url for documentDirectory in userDomainMask appropriateFor nil create false appendi
  • 数据帧的频率

    我有一个用日期索引的数据框 Python 日期时间对象 如何找到数据框中数据的月数频率 我尝试了属性data frame index freq 但它返回无值 我也尝试过asfreq函数使用data frame asfreq M how st
  • 防止 Emacs 滚动到缓冲区末尾

    当我用鼠标或滚动条滚动到缓冲区末尾时 是否可以添加任何内容到 emacs 文件中 以防止 emacs 添加新行 我已经有了 setq next line add newlines nil 但这似乎只适用于我用键盘移动时 我对此主题进行了多次
  • 如何通过单击图标更改 font-awesome 图标的颜色

    var garbage document getElementById garbage garbage addEventListener click function garbage style color 66c144 div i cla