当我使用下面的代码时,我在IE 8-7上收到一个错误,说该对象不支持属性或方法'addEventListener‘。有人知道如何使下面的代码与IE8-7兼容吗?谢谢
document.getElementById('clear').addEventListener('click', function () {
["A1","A2","A3","A4","A5","A6", "A1_flip",
].forEach(function(id) {
document.getElementById(id).checked = false;
});
return false;
})
发布于 2017-04-12 22:33:54
这是因为他们不支持addEventListener
。详情请参见this question's answers。
但是既然您已经说过您正在使用jQuery,那么您就可以使用can...use jQuery来解决这个问题:
$('#clear').on('click', function () {
["A1","A2","A3","A4","A5","A6", "A1_flip"
].forEach(function(id) {
$("#" + id).prop("checked", false);
});
return false;
});
或者实际上,我们可以更直接一点:
$('#clear').on('click', function () {
$("#" + ["A1","A2","A3","A4","A5","A6", "A1_flip"].join(", #")).prop("checked", false);
return false;
});
...which的工作原理是从数组中构建一个选择器字符串。
我刚刚意识到我假设数组的内容是不同的。如果他们不需要,如果你真的想要这些特定的元素:
$('#clear').on('click', function () {
$("#A1, #A2, #A3, #A4, #A5, #A6, #A1_flip").prop("checked", false);
return false;
});
...or更好,给他们一个通用的class
并使用
$('#clear').on('click', function () {
$(".the-class").prop("checked", false);
return false;
});
如果您没有使用jQuery,只是错误地标记了它,请参阅上面的链接问题。其中一个答案是我的,它提供了一个处理跨浏览器事件处理的hookEvent
函数:
var hookEvent = (function() {
var div;
// The function we use on standard-compliant browsers
function standardHookEvent(element, eventName, handler) {
element.addEventListener(eventName, handler, false);
return element;
}
// The function we use on browsers with the previous Microsoft-specific mechanism
function oldIEHookEvent(element, eventName, handler) {
element.attachEvent("on" + eventName, function(e) {
e = e || window.event;
e.preventDefault = oldIEPreventDefault;
e.stopPropagation = oldIEStopPropagation;
handler.call(element, e);
});
return element;
}
// Polyfill for preventDefault on old IE
function oldIEPreventDefault() {
this.returnValue = false;
}
// Polyfill for stopPropagation on old IE
function oldIEStopPropagation() {
this.cancelBubble = true;
}
// Return the appropriate function; we don't rely on document.body
// here just in case someone wants to use this within the head
div = document.createElement('div');
if (div.addEventListener) {
div = undefined;
return standardHookEvent;
}
if (div.attachEvent) {
div = undefined;
return oldIEHookEvent;
}
throw "Neither modern event mechanism (addEventListener nor attachEvent) is supported by this browser.";
})();
然后:
hookEvent(document.getElementById('clear'), 'click', function(e) {
["A1","A2","A3","A4","A5","A6", "A1_flip"
].forEach(function(id) {
document.getElementById(id).prop("checked", false);
});
e.preventDefault();
});
发布于 2017-04-12 22:33:32
对addEventListener()
函数的支持在Internet Explorer的旧版本(如7-8)中不可用,因此您将无法在您尝试使用的浏览器上使用它。
您可以考虑使用jQuery的on()
函数,假设您使用的jQuery版本是针对较老的浏览器设计的,因为它通常会有必要的后备来支持它。
$('#clear').on('click', function () {
var elements = ["A1","A2","A3","A4","A5","A6", "A1_flip"];
elements.forEach(function(id) {
$("#" + id).prop("checked", false);
});
return false;
})
发布于 2017-06-27 11:31:04
如果你想对过时的浏览器有更好的兼容性,那么jQuery的所有新特性都是必需的。您可以考虑直接切换到jQuery 1.1x(当前为1.12)。这将为您节省大量处理兼容问题的时间。
https://stackoverflow.com/questions/43372496
复制相似问题