为什么Evenlistener在jsfiddle上无法正常工作? [英] Why evenlistener not working properly at jsfiddle?

查看:84
本文介绍了为什么Evenlistener在jsfiddle上无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请查看



您需要先绑定事件处理程序它正在监听火灾。将JS Fiddle选项更改为 no wrap选项之一。由于您没有接触DOM中的任何内容,因此选择两个对象中的哪个都没有关系。


Please have a look at jsfiddle or here:

JS:

document.addEventListener('DOMContentLoaded', function myFunction() {
alert("Hello! I am an alert box");
}, false);

why this evenlistener not working? It should work because it works everywhere else. I think it's because of the iframe but I dunno...

Any idea?

解决方案

You have configured JS Fiddle to wrap your code in an onload event handler.

The sequence of events is:

  1. Load event handler is bound.
  2. DOM Content Loaded fires. There is nothing listening for it.
  3. Load event fires.
  4. Load handler executes and binds the DOM Content Loaded listener

You need to bind the event handler before the event it is listening for fires. Change the JS Fiddle options to one of the "no wrap" choices. Since you aren't touching anything in the DOM, it doesn't matter which of the two you select.

这篇关于为什么Evenlistener在jsfiddle上无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆