简体   繁体   English

css 不适用于使用 dom 的图标

[英]css doesn't apply on Icon using dom

Hello I am trying to change the color of icon using dom.您好,我正在尝试使用 dom 更改图标的颜色。 When I do that changing icon to h6 and instead of icon-exclamation use the text.当我这样做时,将 icon 更改为 h6 而不是 icon-exclamation 使用文本。 it works.有用。 How to apply it on the icon: Html code which do it:如何将其应用到图标上:Html 执行此操作的代码:

<div class ="indicator">
                    <i id ="exclamation" class = "icon-exclamation"></i>
                    <h6 id = "pinfo" class ="passwordinfo" >dupa</h6>
            </div>

<script>
            var pass = document.getElementById("password");
            var pinfo =document.getElementById("pinfo");
            var exc = document.getElementById("exclamation");
            console.log(exc[0])
            pass.addEventListener('input', ()=>
            {
    
                if (pass.value.length === 0)
                {
                    pinfo.innerHTML = "Waiting for your password"
                }
                else if (pass.value.length <=4)
                {
                    pinfo.style.visibility = "visible";
                    exc.style.color = "blue";
                    exc.style.display = "block";
                    // exc.style.display = 'block';
                    pinfo.innerHTML = "Password is weak";
                    pinfo.style.color = "#ff0000"
    
    
                }
                else if (pass.value.length >=4 && pass.value.length <8)
                {
                    pinfo.innerHTML = "Password is medium";
                    pinfo.style.color ="#ff8000";
                }
                else
                {
                    pinfo.innerHTML = "Password is strong";
                    pinfo.style.color = "#00ff00";
                }
    
            })
        </script>

Why this code doesn't work and how to change the color of the icon dynamically?为什么此代码不起作用以及如何动态更改图标的颜色?

Your code works correctly, you might have not included all the assets including fonts and CSS files.您的代码工作正常,您可能没有包含所有资产,包括 fonts 和 CSS 文件。 You can have a look at the working code here: https://codesandbox.io/s/mutable-sea-fl3320?file=/src/index.js您可以在这里查看工作代码: https://codesandbox.io/s/mutable-sea-fl3320?file=/src/index.js

在此处输入图像描述

You can read more about how to use fontello icons in your code here .您可以在此处阅读有关如何在代码中使用 fontello 图标的更多信息。

You can use fontawesome instead of fontello , of course.当然,您可以使用fontawesome而不是fontello It's more simple and more robust for most situations.对于大多数情况,它更简单、更健壮。 In your HTML file, please add the first line to add the fontawesome CSS library, and instead of the commented line, try to use fontawesome icon.在您的 HTML 文件中,请添加第一行以添加fontawesome CSS 库,而不是注释行,尝试使用fontawesome图标。

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<div class="indicator">
  <input id="password" />
  <input id="pinfo" />

<!--   <i  class="icon-exclamation"></i> -->
  <i id="exclamation" class="fa fa-exclamation-circle fa-2x"></i>
  <h6 id="pinfo" class="passwordinfo">dupa</h6>
</div>

在此处输入图像描述

If i well understood, if you want to change dinamically the icon color as you change the pinfo, you just have to put exc.style.color inside the ifs you want to change..如果我很好理解,如果您想在更改 pinfo 时动态更改图标颜色,您只需将exc.style.color放在要更改的 ifs 中。

May be something like this:可能是这样的:

pass.addEventListener('input', () => {
            if (pass.value.length === 0)
            {
                pinfo.innerHTML = "Waiting for your password"
            }
            else if (pass.value.length <=4)
            {
                pinfo.style.visibility = "visible";
                exc.style.color = "blue";
                exc.style.display = "block";

                pinfo.innerHTML = "Password is weak";
                pinfo.style.color = "#ff0000"
            }
            else if (pass.value.length >=4 && pass.value.length <8)
            {
                pinfo.innerHTML = "Password is medium";
                pinfo.style.color ="#ff8000";
                exc.style.color = "orange";
            }
            else
            {
                pinfo.innerHTML = "Password is strong";
                pinfo.style.color = "#00ff00";
                exc.style.color = "yellow";
            }

        })

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

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