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

时间:2015-11-19 21:56:19

标签: javascript internet-explorer-8

我有一个函数,只需根据点击更改元素的类。除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>

2 个答案:

答案 0 :(得分:1)

看起来你正在使用jQuery,它提供跨浏览器支持来操作元素上的类。 IE 8中也不支持forEach

classList browser support

forEach browser support

的jQuery

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

JSFiddle(没有jQuery并支持IE8)

答案 1 :(得分:0)

classListnot supported by IE8 or 9。由于您已经在使用jQuery,因此您应该使用:jQuery.toggleClass

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