简体   繁体   English

单击时无法更改 Font Awesome 图标?

[英]Can't change Font Awesome icon on click?

I am trying to change a font awesome icon using a toggle, however, nothing seems to be working and i am not sure why?我正在尝试使用切换更改字体真棒图标,但是,似乎没有任何效果,我不知道为什么?

(HTML) (HTML)

<span class="change-icon"><i class="far fa-eye" id="password-see"></i></span></div>

(CSS) (CSS)

.change-icon {
    cursor: pointer;
}

(Javascript) (Javascript)

$(document).ready(function () {
    $('change-icon').click(function () {
        $('i').toggleClass('far fa-eye far fa-eye-slash');
    });
});

Any help would be greatly appreciated!任何帮助将不胜感激!

Your problem is that you try to get the html element .change-icon but you try to get it with $('change-icon') instead of $('.change-icon') where the dot is representing that it is a class您的问题是您尝试获取 html 元素 .change-icon 但您尝试使用 $('change-icon') 而不是 $('.change-icon') 来获取它,其中点表示它是一个班级

Change this:改变这个:

$(document).ready(function () {
    $('change-icon').click(function () {
        $('i').toggleClass('far fa-eye far fa-eye-slash');
    });
});

To this:对此:

$(document).ready(function () {
    $('.change-icon').click(function () {
        $('i').toggleClass('fa-eye fa-eye-slash');
    });
});

And i would change $('i') to $('#password-see')我会将 $('i') 更改为 $('#password-see')

Try this尝试这个

$(document).ready(function () {
    $('.change-icon').click(function () {
        $('i').toggleClass('fa-eye fa-eye-slash');
    });
});

Try this bro试试这个兄弟

 $(document).ready(function () {
        $('.change-icon').click(function (event) {
            $(event).find('i').toggleClass('far fa-eye far fa-eye-slash');
        });
    });

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

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