問題描述
我正在嘗試根據(jù)以下描述了解 JSFiddle 如何在onLoad"中包裝"代碼:[1]:http://doc.jsfiddle.net/basic/introduction.html#javascript.我已經(jīng)看到在 HTML 的 BODY 標記中使用 onLoad 來加載函數(shù).那么 JSF(在幕后)是否將我在其 JS 窗口中創(chuàng)建的每個調(diào)用和每個函數(shù)都包裝起來?又名:
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{(...)}
...
如果有電樞問題,請?zhí)崆暗狼?
Apologize in advance if an armature question.
推薦答案
他們把你所有的 JS 放在 <script>
標簽內(nèi),并用 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
事件,具體取決于您選擇包含的內(nèi)容.
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
這篇關(guān)于JSFiddle包裝在onLoad中?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!