javascript,无法在相同 class 的所有元素上更改 class

javascript, can't change class on all elements of the same class

我需要从 i html 元素中删除所有 类。我有一个 table 每行有很多元素。他们是:

<i class="fa fa-trash fa-fw"></i>

我想根据条件更改它们,通过 javascript,像这样:

<script>
    if(1){//doesn't matter what 'if' is, it's working this part
        document.getElementsByClassName('fa-trash').className = '';
    }
</script>

但是什么也没发生。当我使用 getElementById 时它有效,但我不想使用 id,因为我有很多元素。我做错了什么?

getElementsByClassName 方法 returns 一个 collection 作为 NodeList 对象。 NodeList 对象表示节点的集合。因此,您需要为查询结果中的每个项目更新 className。为此,您可以使用 Array.from 方法。

The Array.from() method creates a new Array instance from an array-like or iterable object.

Array.from(document.getElementsByClassName('fa-trash')).forEach(function(item){
    item.className = '';
});