javascript JSFiddle 包裹在 onLoad 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19203922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSFiddle wrap in onLoad?
提问by brooklynsweb
I'm trying to understand how JSFiddle 'wraps' code in 'onLoad' based on this description: [1]: http://doc.jsfiddle.net/basic/introduction.html#javascript. I've seen onLoad being used in the HTML's BODY tag to load function(s). So does JSF (behind the scenes) wrap every call and every function I create int its JS window? AKA:
我试图根据以下描述了解 JSFiddle 如何在“onLoad”中“包装”代码:[1]:http://doc.jsfiddle.net/basic/introduction.html#javascript 。我已经看到在 HTML 的 BODY 标签中使用 onLoad 来加载函数。那么 JSF(在幕后)是否将我在其 JS 窗口中创建的每个调用和每个函数都包装起来?又名:
onLoad = "myfunc1(),myfunc2,alert(1);"
If so then when I select jQuery as a framework, should I avoid using this format:
如果是这样,那么当我选择 jQuery 作为框架时,我是否应该避免使用这种格式:
$(document).ready(function(){
myfunc1{(...)}
myfunc1{(...)}
...
Apologize in advance if an armature question.
如果是电枢问题,请提前道歉。
回答by Joe
They put all your JS inside <script>
tags with the onLoad
event code wrapped around it.
他们将您所有的 JS 放在<script>
标签中,并onLoad
在其周围包裹事件代码。
For example, if you choose to include jQuery and onLoad
then this is what jsfiddle will use:
例如,如果您选择包含 jQuery,onLoad
那么这就是 jsfiddle 将使用的:
<script type="text/javascript">
//<![CDATA[
$(window).load(function(){ /* your js here */ });
//]]>
</script>
If you don't include a library then they use:
如果您不包含库,则他们使用:
<script type="text/javascript">
//<![CDATA[
window.onload=function(){ /* your js here */ }
//]]>
</script>
I presume they also use other library specific load
events depending on what you choose to include.
我认为他们还使用其他特定load
于库的事件,具体取决于您选择包含的内容。
Using $(document).ready(function(){ });
isn't required when running your code in a fiddle.
$(document).ready(function(){ });
在小提琴中运行代码时不需要使用。
Note: for a good explanation of what CDATA
is take a look at this answer - https://stackoverflow.com/a/7092306/2287470
注意:关于什么CDATA
是一个很好的解释,看看这个答案 - https://stackoverflow.com/a/7092306/2287470
回答by Joe Simmons
It makes a new iframe to run your code in.
它创建了一个新的 iframe 来运行您的代码。
For the onload, they just put all your code in
对于 onload,他们只是将您所有的代码放入
window.onload = function () {
// your code here
};
You can see this if you inspect the <script>
tag in the <head>
of the bottom-right iframe.
如果您检查右下角 iframe 中的<script>
标记,您就会看到这一点<head>
。
Nothing wrong with using $(document).ready(...
but it's kinda useless when you enable the onLoad option in jsFiddle.
使用没什么问题,$(document).ready(...
但是当您在 jsFiddle 中启用 onLoad 选项时,它有点没用。
回答by user1667253
if you are looking for source code formatting, then you should look for site like below. As to what jsfiddle use they might have their own plugins which does the same.
如果您正在寻找源代码格式,那么您应该寻找如下所示的站点。至于 jsfiddle 使用什么,他们可能有自己的插件来做同样的事情。
http://www.javascriptbeautifier.com/