InternetExplorer中的addEventListener与InternetExplorer 9中的元素对象等效的是什么?if (!Element.prototype.addEventListener) {
Element.prototype.addEventListener = function() { .. } }它是如何在InternetExplorer中工作的?如果有一个等于addEventListener我不知道,请解释一下。任何帮助都将不胜感激。可以自由地提出一个完全不同的解决问题的方法。
3 回答
哔哔one
TA贡献1854条经验 获得超8个赞
addEventListener
attachEvent
addEventListener
addEvent
function addEvent(evnt, elem, func) {
if (elem.addEventListener) // W3C DOM
elem.addEventListener(evnt,func,false);
else if (elem.attachEvent) { // IE DOM
elem.attachEvent("on"+evnt, func);
}
else { // No much to do
elem["on"+evnt] = func;
}}
胡说叔叔
TA贡献1804条经验 获得超8个赞
if (typeof Element.prototype.addEventListener === 'undefined') {
Element.prototype.addEventListener = function (e, callback) {
e = 'on' + e;
return this.attachEvent(e, callback);
};
}<button class="click-me">Say Hello</button><script>
document.querySelectorAll('.click-me')[0].addEventListener('click', function () {
console.log('Hello');
});</script>添加回答
举报
0/150
提交
取消
