Javascript 使用 .classList 和 .toggle 的函数在 IE8 中不工作

Javascript function which uses .classList and .toggle is not working in IE8

我有一个函数可以根据点击简单地改变元素的 class。它在任何地方都可以正常工作,但 IE8。任何帮助都会很棒!

这是函数:

function attachToggleReportType (elem) {
    elem.addEventListener('change', toggleReportType);
}

function toggleReportType () {
var reportOptions = document.querySelectorAll('.report'),
    reportIncToggle = document.querySelectorAll('.toggle');

reportSample({
    href: this.getAttribute('data-sample-report-link'),
    src: this.getAttribute('data-sample-report-image')
});

reportIncToggle.forEach(toggleInclude);

    var report_type = $(this).find('input[type=radio]').val();
    report_type = ( report_type == 1 ? 'Verified' : 'Claims');
    // analytics.updateType(report_type);
}

function toggleInclude (item) {
    item.classList.toggle('notIncluded');
    item.classList.toggle('included');
}

HTML

<li class="larger toggle notIncluded">
    <span>Cross-Canada lien search</span>
    <br>
    Exclusive to CARPROOF <strong>Verified</strong> reports, we’ll tell you if there’s money owing on the vehicle.
</li>

classList is not supported by IE8 or 9. Since you are already using jQuery, you should just use: jQuery.toggleClass.

$(item).toggleClass('notIncluded included');

您似乎在使用 jQuery,它为在元素上操作 类 提供跨浏览器支持。另外 forEach 不支持 IE 8

classList browser support

forEach browser support

jQuery

$('.elementClass').toggleClass('className');

JSFiddle(不带jQuery,支持IE8)