【问题标题】:How to change color of font-awesome icon by clicking the icon如何通过单击图标更改字体真棒图标的颜色
【发布时间】:2018-09-26 03:01:20
【问题描述】:

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

【问题讨论】:

  • 你必须改变 i 颜色而不是 div
  • 此代码有语法错误。另外,你的班级名称是fas,如果我没记错的话应该是fa。最后,您设置了 div 而不是 i 元素的样式,这可能会被覆盖。
  • @Roberrrt Op 正在使用 Font Awesome 5,它具有 fas 前缀
  • @MatheusCuba 啊,我明白了!

标签: javascript css font-awesome


【解决方案1】:

您在关闭} 后缺少);

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。 p>

【讨论】:

    【解决方案2】:

    以下是更改 FA 图标的其他一些选项。关键字this 代表garbage.property.method()/.function() 引用该图标。

    参考文献

    .children

    .querySelector()

    .getElementsByTagName()

    .classList

    .firstElementChild


    演示

    var garbage = document.getElementById("garbage");
    
    garbage.addEventListener("click", function() {
      this.children[0].style.color = "#66c144";
      this.querySelector('.fas').style.fontSize = '3rem';
      this.getElementsByTagName('I')[0].classList.toggle('fa-spin');
      this.firstElementChild.style.transition = 'color 1.5s ease 1.25s, font-size 0.75s ease-out';
    }, false);
    <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>

    【讨论】:

    • 天啊,你是救生员。我必须习惯使用所有这些方法。非常感谢!
    • 太好了,我打算添加另一种通过事件对象引用的方式,但我担心它可能会造成混淆。如果您有兴趣使用event.target 作为参考,请阅读此article,当您了解事件要点时,请阅读此one
    • 幸运的是,我能够掌握这个概念。我还没有理解 100%,例如“错误”部分(我想这会阻止图标旋转?)。 event.target 似乎超级有用,因为它上次帮助我解决了“如何删除对象”问题。我会读的;我也更新了我的最终结果,但你能看看是否有一种不那么冗余的方法吗?谢谢。
    • 嗨@YongJunJung 假的部分是骗人的,它是addEventListener() 的第三个可选参数,默认为假。当为 false 时,eventListener 将在冒泡阶段调用回调(在目标阶段之后,如单击或悬停 - 它非常短)。 True 表示将在捕获阶段(目标阶段之前)调用回调函数。捕获阶段很少使用,我发现它唯一有用的是处理关键事件(keyup,keydown)。使图标停止旋转的原因是它是一个被切换的类,fa-spin 是一个 FA 特殊类。
    • 在主帖中查看我的评论,另外你应该就你的下一个问题再发一个帖子,否则版主会抱怨你帖子上的多个问题。
    【解决方案3】:

    您的事件侦听器后面缺少右括号:

    var garbage = document.getElementById("garbage");
    
    garbage.addEventListener("click",function(){
        garbage.style.color = "#66c144";
    });
    

    【讨论】:

    • 当我比其他人早一分钟发布完全相同的答案时,我真的被否决了?难以置信
    • 谢谢。哈这不公平!
    • 我给了你 +1,但你可能在
    • @VXp Sam Littlefair 没有编辑它,头像左侧没有链接,他比你早 1 分钟回答。无论如何,我提升了你们两个。 Littlefair 先生答对了,你答对了minimal reproducible example
    • @zer00ne 由于有用的附加信息和总体上好的答案而返回了“青睐”。我知道,这就是我写“under 5min”的原因,因为如果你在那个时间范围内这样做,链接就不会出现。我认为他做到了,否则他为什么会得到-2。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-19
    • 2021-04-20
    • 1970-01-01
    • 2016-04-08
    • 2021-10-24
    • 2017-02-19
    • 2017-04-23
    相关资源
    最近更新 更多