JSFiddle包装在onLoad中?

2022-01-16 00:00:00 frameworks jquery javascript jsfiddle

我正在尝试根据以下描述了解 JSFiddle 如何在onLoad"中包装"代码:[1]:http://doc.jsfiddle.net/basic/introduction.html#javascript.我已经看到在 HTML 的 BODY 标记中使用 onLoad 来加载函数.那么 JSF(在幕后)是否将我在其 JS 窗口中创建的每个调用和每个函数都包装起来?又名:

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:

onLoad = "myfunc1(),myfunc2,alert(1);" 

如果是这样,那么当我选择 jQuery 作为框架时,我是否应该避免使用这种格式:

If so then when I select jQuery as a framework, should I avoid using this format:

 $(document).ready(function(){
     myfunc1{(...)}
     myfunc1{(...)}
     ...

如果有电枢问题,请提前道歉.

Apologize in advance if an armature question.

推荐答案

他们把你所有的 JS 放在 <script> 标签内,并用 onLoad 事件代码包裹它.

They put all your JS inside <script> tags with the onLoad event code wrapped around it.

例如,如果您选择包含 jQuery 和 onLoad,那么这就是 jsfiddle 将使用的:

For example, if you choose to include jQuery and onLoad then this is what jsfiddle will use:

<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>

我认为他们还使用其他库特定的 load 事件,具体取决于您选择包含的内容.

I presume they also use other library specific load events depending on what you choose to include.

在小提琴中运行代码时不需要使用 $(document).ready(function(){ });.

Using $(document).ready(function(){ }); isn't required when running your code in a fiddle.

注意:要更好地解释 CDATA 是什么,请查看此答案 - https:///stackoverflow.com/a/7092306/2287470

Note: for a good explanation of what CDATA is take a look at this answer - https://stackoverflow.com/a/7092306/2287470

相关文章