简体   繁体   English

once:true,addEventListener不适用于IE11或Edge

[英]once:true with addEventListener doesn't work on IE11 or Edge

I'm trying to fire an event listener callback just once. 我试图只触发一次事件监听器回调。 addEventListener conveniently has a flag that I can pass into it: addEventListener方便地有一个我可以传递给它的标志:

once: A Boolean indicating that the listener should be invoked at most once after being added. once:一个布尔值,表示在添加后最多应调用一次监听器。 If it is true, the listener would be removed automatically when it is invoked. 如果为true,则在调用侦听器时将自动删除该侦听器。

This works fantastic in every browser except IE11 and Edge. 除了 IE11和Edge 之外,这在每个浏览器中都很棒。 I know that I can manually remove the event listener after it fires, but this flag should work... Am I doing something wrong, or is this an acknowledged issue with IE or what? 我知道我可以在它触发后手动删除事件监听器,但是这个标志应该工作......我做错了什么,或者这是IE的公认问题还是什么?

Should you prefer JSFiddle , otherwise: 你应该更喜欢JSFiddle ,否则:

 var div = document.getElementById('transition'); var result = document.getElementById('result'); window.grow = function(){ var width = div.offsetWidth + 50; div.style.width = width + 'px'; } var count = 0; div.addEventListener('transitionend', function(){ result.innerHTML = "transitionend has fired " + ++count + " times"; }, { once: true }); 
 #transition{ background-color: yellow; transition: width 2s; width: 100px; } 
 <div id="transition"> Content </div> <button onclick="grow()"> Grow the div </button> <div id="result"> </div> 

Check the browser compatibility section of the docs : no IE/edge support. 检查文档的浏览器兼容性部分:没有IE / edge支持。 caniuse is also helpful. caniuse也很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM