javascript - 如何通过单击图标更改超赞字体图标的颜色

标签 javascript css font-awesome

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>

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


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>
更新 所以感谢大家的帮助,我终于得到了我想要的结果(每次点击不同的图标,只有我点击的图标有不同的颜色)。最重要的是,我很好奇是否有一种不那么重复或困惑的方式来执行我现在喜欢使用“功能”或其他东西的方式。如果我能得到一些提示,我将不胜感激。

最佳答案

您缺少 ); 在结束 } 之后:

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>

注意:您不需要定位父级,您只需将id提供给图标。

关于javascript - 如何通过单击图标更改超赞字体图标的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49856171/

相关文章:

javascript - 用于验证字段的正则表达式

javascript - 如何将图像应用到 Cordova 中的选项卡栏项目

javascript - JavaScript 日期函数的 IE 字符集编码问题

html - SVG <a> 元素内的 <text> 元素在 IE 中没有下划线

html - 如何在 Bootstrap 中以相同的内联形式更改输入框的大小

javascript - 谷歌闭包编译器不重命名属性

jquery - 悬停时多个元素之间的无缝淡入淡出(jquery)

html - 保留:after element on same line

javascript - Font Awesome 堆叠图标悬停轮廓问题

css - 我可以覆盖现有的 Font Awesome CSS 图标吗?