我有一個函數,它可以根據點擊簡單地更改元素的類。它工作正常,但IE8。任何援助將是偉大的!使用.classList和.toggle的JavaScript函數在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,只需使用'toggleClass()'。它將克服瀏覽器的不一致性。 –
['classList'是IE8不支持的許多東西之一](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList#Browser_compatibility)。而IE9也沒有。要麼加載一個polyfill,至少存在一個,要麼使用jQuery或其他庫。 –
[code classList無法在IE中運行?](http://stackoverflow.com/questions/8098406/code-with-classlist-does-not-work-in-ie) –