简体   繁体   English

单击时更改 font-awesome 的类名

[英]Change class name of font-awesome when clicked

Here is the problem这是问题所在

This is my html code这是我的html代码

        <div id="bookmark">
            <i class="far fa-bookmark"></i>
        </div>

I want to change .far to .fas and vice versa in loop when clicked.我想改变.far.fas中,反之亦然loop点击时。

This is my updated javascript这是我更新的 javascript

function changeClass(elem) {
    var i = elem.childNodes[1];
    var c = i.classList;
    console.log(i)
    console.log(c)

    // Change class
    if (c.contains("far")) {
        i.classList.remove("far");
        i.classList.add("fas");
    } else {
        i.classList.remove("fas");
        i.classList.add("far");
    }
}

This is the screenshot of the console output in the browser when I click on the bookmark icon这是我点击书签图标时浏览器控制台输出的截图

Screenshot截屏

When I pass the index value in the var c respectively, on i.classList[0] I get svg-inline--fa but I get undefined value in i.classList[3] instead of far .当我分别在var c传递索引值时,在i.classList[0]我得到svg-inline--fa但我在i.classList[3]而不是far得到undefined值。 SO WHAT SHOULD I DO?所以我该怎么做?

You can do it like below with jQuery:您可以使用 jQuery 执行以下操作:

$("#bookmark").find("i").removeClass("far");
$("#bookmark").find("i").addClass("fas");

assuming you want this to be done for single element and not for all the elements that are using .far class假设您希望对单个元素执行此操作,而不是对所有使用 .far 类的元素执行此操作

add an id to the tag like id="i1" then you can add an event listener on that element.向标签添加一个 id,如 id="i1" 然后您可以在该元素上添加一个事件侦听器。 listening to the click event and once captured - toggle the class.收听点击事件,一旦被捕获 - 切换类。

add the following just before the end of the body tag ie before </body>在 body 标签的末尾添加以下内容,即在</body>之前

<script>
document.getElementById('i1').addEventListener('click', (e) => {
    e.target.classList.toggle('far');
    e.target.classList.toggle('fas');
})
</script>

I hope this is what you are looking for (Demo: https://jsfiddle.net/hwv0oacL/8/ )我希望这就是你要找的(演示: https : //jsfiddle.net/hwv0oacL/8/

HTML HTML

<div id="bookmark" onclick="changeClass(this)">
    <i class="far fa-bookmark"></i>
</div>

Javascript Javascript

changeClass(elem) {
    var i = elem.childNodes[0].classList;
    
    // Change class far to fas
    if (i.includes("far")) {
        i.classList.remove("far");
        i.classList.add("fas");
    }

    // Get back to original state
    if (i.includes("fas")) {
        i.classList.remove("fas");
        i.classList.add("far");
    }
}

-------------------Updated-------------------------- - - - - - - - - - -更新 - - - - - - - - - - - - -

Replace the above javascript with the one below.用下面的替换上面的javascript。 It will also work in loop like you want because we are getting element by this keyword.它也可以像您想要的那样在循环中工作,因为我们正在通过this关键字获取元素。

JavaScript JavaScript

function changeClass(elem) {
    var i = elem.childNodes[1];
    var c = i.classList;

    // Change class
    if (c.contains("far")) {
        i.classList.remove("far");
        i.classList.add("fas");
    } else {
        i.classList.remove("fas");
        i.classList.add("far");
    }
}

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

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