简体   繁体   English

如何在点击时更改字体真棒图标

[英]how to change font awesome icon on click

I'm using this script我正在使用这个脚本

<audio src="http://193.108.24.21:8000/fresh" id="audio"></audio>
    <i class='fas fa-play fa-5x' id="play" onclick="play(this)"></i>

    <script>
    function play(button) {
    var audio = $(button).prev()[0];
    if (audio.paused) {
        audio.play();
        $('#play').removeClass('fa-play')
        $('#play').addClass('fa-pause')
    }else{
        audio.pause();
        audio.currentTime = 0
        $('#play').addClass('fa-play')
        $('#play').removeClass('fa-pause')
    }
}
    </script>

but when I add several to one page it doesn't work.但是当我将几个添加到一页时它不起作用。

image图片

This is what happens, each player works for itself, but the icons do not.这就是发生的事情,每个玩家都为自己工作,但图标却没有。 When one player is clicked, only one icon changes as in the picture当一个玩家被点击时,只有一个图标改变,如图

image2图2

How can I make each icon stand out for itself?如何让每个图标脱颖而出? I changed the id for each one individually but it didn't happen again.我单独更改了每个人的 id,但它没有再次发生。 Thanks!谢谢!

Use $(button) instead of $("#play") to target the clicked button instead of an id, which won't work because it's not unique.使用 $(button) 而不是 $("#play") 来定位单击的按钮而不是 id,因为它不是唯一的,所以它不起作用。

    <script>
    function play(button) {
    var audio = $(button).prev()[0];
    if (audio.paused) {
        audio.play();
        $(button).removeClass('fa-play')
        $(button).addClass('fa-pause')
    }else{
        audio.pause();
        audio.currentTime = 0
        $(button).addClass('fa-play')
        $(button).removeClass('fa-pause')
    }
}
    </script>

also you could use.toggleClass('fa-play fa-pause') instead of adding and removing the classes manually.您也可以使用 .toggleClass('fa-play fa-pause') 而不是手动添加和删除类。

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

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