jsFiddle onload和no-wrap in body [英] jsFiddle onload and no-wrap in body

查看:73
本文介绍了jsFiddle onload和no-wrap in body的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图让自己开始使用jsFiddle。所以我尝试运行一个简单的代码片段,其中包含所有HTML,CSS和JavaScript代码。

I am trying to get myself started with jsFiddle. So I tried to run a simple code snippet which includes all HTML, CSS and JavaScript code.

Javascript不起作用如果我选择onLoad在框架和&扩展程序下拉列表

Javascript does not works If I select onLoad in Frameworks & Extensions dropdown

但是当我从下拉列表中选择No Wrap - in时它确实有效

But it does work when I select No Wrap - in from the dropdown

你能告诉我这是什么意思吗?我已经在SO JavaScript未在jsfiddle.net上运行上阅读了这个问题

Can you tell me what that means . I have already read this question on SO JavaScript not running on jsfiddle.net

但无法理解那里提到的解决方案。

But not able to understand the solution mentioned there.

推荐答案

你选择onLoad,你的JavaScript包含一个onload函数。这意味着您的代码将在页面加载完成后运行,但在全局范围内不再可用。看起来像这样

When you select onLoad, your JavaScript is wrapped with an onload function. This means your code will run when the page has finished loading, but is no longer available in the global scope. It looks like this

window.onload=function(){
   function myFunction() {
       alert("Hello");
   }
}

解决方法可能是将变量分配给 window object,以便可以在页面的任何位置访问它们。

A workaround might be to assign variables to the window object so that they are accessible anywhere in the page.

例如:

function myFunction() {
    alert("Hello");
}
window.myFunction = myFunction;

<button onclick="window.myFunction()" >Hi</button>

这篇关于jsFiddle onload和no-wrap in body的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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