在Firefox问题上使用JavaScript循环遍历数组

时间:2015-09-22 14:02:27

标签: javascript html firefox

我在这里有一些javascript基本上循环遍历我页面上的某些值,并根据值添加一个类。这适用于chrome和safari,但在Firefox上它看起来并没有像它应该的那样工作。我猜是有一种不同的方法来为firefox编写它以正确渲染它,但似乎无法确定问题所在。我知道这是jQuery可以为你处理的东西,但我想只使用javascript。

function ranks() {

 var scores = document.querySelectorAll('.MyScore'); // get all elements with .MyScore

 Array.prototype.forEach.call(scores, function (item) { // iterate the elements

    var score = parseInt(item.innerText, 10); // get the value from the text and parse it

    if (score > 80) {
        item.classList.add('great'); // add great to the item classList


    } else if (score > 60 && score < 80) {
        item.classList.add('okay'); // add okey to the item classList

    } else if(score == 0) {
        item.classList.add('no-score'); // add bad to the item classList

    } else {
        item.classList.add('bad'); // add bad to the item classList

    }
});


}
ranks();

您可以在不同浏览器上对此jsbin进行比较,看看我的意思。

0 个答案:

没有答案