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

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

本文关键字:IE8 不起作用 函数 Javascript classList togle 使用      更新时间:2023-09-26

我有一个函数,它只是根据单击来更改元素的类。除了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>

看起来您正在使用jQuery,它为在元素上操作类提供了跨浏览器支持。此外,IE 8 中不支持forEach

classList浏览器支持

forEach浏览器支持

jQuery

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

JSFiddle(不带jQuery,支持IE8)

classList不受IE8或9的支持。由于您已经在使用jQuery,您应该只使用:jQuery.toggleClass.

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