onLoad,onDomready,No wrap-in< head>和No wrap-in< body>?之间有什么区别? [英] What is the difference between onLoad, onDomready, No wrap - in <head>, and No wrap - in <body>?
问题描述
我使用JSFiddle编辑我的代码。但是,在某些代码中,当我运行JavaScript或jQuery时,它不起作用,除非我选择No wrap - < head>
或No wrap - < body>
。
I use JSFiddle for editing my code. However, in certain codes when I'm running JavaScript or jQuery, it doesn't work unless I select "No wrap - <head>
" or "No wrap - <body>
".
在上面的小提琴中,您会注意到点击< button>
元素将不会 alert()
除非你选择了扩展名No wrap - < head>
或没有换行 - < body>
。
In the fiddle above, you will notice that clicking the <button>
element will not alert()
you unless you've selected either the extension "No wrap - <head>
" or "No wrap - <body>
".
我是个好奇的人喜欢了解事情是如何运作的。该选项究竟发生了什么变化?为什么要更改它?
I'm a curious person who likes to understand how things work. What exactly does that option change, and why would you change it?
推荐答案
onLoad和onDomready包装代码以便JavaScript在文档加载或DOM就绪事件触发时运行。这就像你编写的代码一样:
The onLoad and onDomready wrap the code so that the JavaScript runs when the document load or DOM ready events fire. It's as if your wrote your code like this:
<script type="text/javascript">
//<![CDATA[
window.onload=function(){ /* your js here */ }
//]]>
</script>
如果您将脚本标记添加到文档的头部或正文标记中,则无包装选项就像你可能习惯这样做。
The no wrap options are if you added your script tag to the head or the body tags of the document like how you're probably used to doing it.
<html>
<head>
<title>Stuff</title>
<script>
/* your code */
</script>
</head>
这篇关于onLoad,onDomready,No wrap-in< head>和No wrap-in< body>?之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!