简单的onload在JSFIddle中不起作用 [英] Simple onload doesn't work in JSFIddle

查看:89
本文介绍了简单的onload在JSFIddle中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



加载时,您已经配置JSFiddle来运行JS。 / code>事件触发。



因此,当载入事件触发时,您将绑定另一个 load 事件处理程序。



您的新的 load 因为 load 事件已经被触发,因此被调用。



将菜单选项更改为无包装方法之一。




第二个问题:

load 事件触发窗口对象,而不是 body 元素。 / p>

您需要将该属性分配到正确的位置。

  onload = function(){
alert(LOADED!);
}

例如: https://jsfiddle.net/n5jb0159/5/


http://jsfiddle.net/jzhang172/n5jb0159/

Simple document.body.onload event should trigger a javascript alert, but doesn't work but it works fine in my text editor -> browser. I switched the libraries around to jQuery to None (pure JS), still nothing, can someone explain to me what's going on and why it doesn't work in fiddle but works fine in my text editor?

This works:

  <body>
    Why
    <script>
  document.body.onload = function(){
      alert("LOADED!");
  }
  </script>
  </body>

解决方案

Your first problem:

You have configured JSFiddle to run your JS when the load event fires.

Consequently, when the load event fires, you bind another load event handler.

Your new load event handler is never called because the load event has already fired.

Change the menu option to one of the "No Wrap" approaches.


Your second problem:

The load event fires on the window object, not the body element.

You need to assign the property to the right place.

onload = function(){
  alert("LOADED!");
}

Such: https://jsfiddle.net/n5jb0159/5/

这篇关于简单的onload在JSFIddle中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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