【问题标题】:Cross browser addEventListener跨浏览器 addEventListener
【发布时间】:2017-01-02 08:39:37
【问题描述】:

我使用了多个 EventListener,但这个特定的在 IE 中不起作用:

document.querySelector('#assets').addEventListener('loaded', function () {
 document.getElementById('startbutton').style.display = "inline";
});

document.querySelector('#assets').addEventListener('loaded', function () {
 document.getElementById('loadingbutton').style.display = "none";
}); 

我想在加载资产时显示一个按钮,然后在加载资产后立即删除不显示的按钮。问题是加载按钮没有被删除,因此没有应用样式。这只发生在 Internet Explorer 中。我如何使用 attachEvent 编写它以便它工作?

按钮 html:

<button id="startbutton" class="my_popup_close button" onclick="playSound();setTimeout(showDiv, 2000);" name="answer" value="Show Div" style="display: none;">Ready</button>

<button class="buttonloading loader" id="loadingbutton">Loading<span class="loader__dot">.</span><span class="loader__dot">.</span><span class="loader__dot">.</span></button>

【问题讨论】:

  • 监听的两个事件是一样的,结合回调命令,自己省几个字节。
  • 你有工作代码示例吗?

标签: javascript html button event-handling


【解决方案1】:

监听的元素是一样的,事件也是一样的。你应该像这样组合回调:

document.querySelector('#assets').addEventListener('loaded', function (){
   // display #startbutton and hide #loadingbutton
   document.getElementById('startbutton').style.display = "inline";
   document.getElementById('loadingbutton').style.display = "none";
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 2011-11-15
    • 2011-06-08
    • 2011-03-03
    • 2012-09-30
    • 2013-05-12
    相关资源
    最近更新 更多