gpt4 book ai didi

javascript - 一次 :true with addEventListener doesn't work on IE11 or Edge

转载 作者:行者123 更新时间:2023-11-29 10:33:15 25 4
gpt4 key购买 nike

我试图只触发一次事件监听器回调。 addEventListener方便地有一个我可以传递给它的标志:

once: A Boolean indicating that the listener should be invoked at most once after being added. If it is true, the listener would be removed automatically when it is invoked.

这在除 IE11 和 Edge 之外的所有浏览器中都非常有效。。我知道我可以在事件监听器触发后手动删除它,但是这个标志应该可以工作...我做错了什么,或者这是 IE 的公认问题还是什么?

Should you prefer 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>

最佳答案

查看 docs 的浏览器兼容性部分: 没有 IE/edge 支持。 caniuse也很有帮助。

关于javascript - 一次 :true with addEventListener doesn't work on IE11 or Edge,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41402991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com