我无法让 .addClass 在 IE11 中工作

I can't get .addClass to work in IE11

function redrawButtonToolbar() {
    //debugger;
    var buttonYears = window.availableButtonYears;

    $(".year-btn").removeClass('hidden').each(function(index) {
        var yearBtn = $(this).find('input').val();
        console.log("buttonYears: " + buttonYears + " yearBtn: " + yearBtn);

        if(yearBtn === 'all') {
            return
        }

        if(!buttonYears.indexOf(yearBtn) !== -1) {
            $(this).addClass('hidden')
            return
        }

    })

}

** 从另一个函数调用

window.availableButtonYears = _.uniq(_.map(_.filter(window.data, selectionFilter), "Year"));

当我 运行 调试器时,我得到以下值存储在 window.availableButtonYears 2014、2015、2016

以上代码在 Chrome 中运行良好,但在 IE11 中不起作用。

已更新 来自 console.log

buttonYears: 2014,2015,2016 yearBtn: all
buttonYears: 2014,2015,2016 yearBtn: 2017
buttonYears: 2014,2015,2016 yearBtn: 2016
buttonYears: 2014,2015,2016 yearBtn: 2015
buttonYears: 2014,2015,2016 yearBtn: 2014

问题是 indexOf returns -1 如果找不到任何东西。不一定是假的。

    if(buttonYears.indexOf(yearBtn) !== -1) {

检查您对 indexOf 的使用。它似乎不符合通常使用该功能的方式,这让我怀疑你误用了它。

此代码意味着您具体检查 "yearBtn" 是否在索引 0 处:

if(!string.indexOf("needle"))

相当于这段代码:

if(string.indexOf("needle") == 0) //the string is at index 0

这样做意味着您要检查 "yearBtn" 是否存在于字符串中:

if(string.indexOf("needle") >= 0) //the string is present

if(string.indexOf("needle") < 0) //the string is not present