<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>$.ready() &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/ready/feed/" rel="self" type="application/rss+xml" />
	<link>https://noter.tw</link>
	<description>一路上踩到的坑、遇到的問題，一點一滴記下來，希望能幫助到需要的人~</description>
	<lastBuildDate>Wed, 04 Dec 2019 01:23:05 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.3</generator>

<image>
	<url>https://noter.tw/wp-content/uploads/cropped-old-1130742_1920-1-32x32.jpg</url>
	<title>$.ready() &#8211; 記下來</title>
	<link>https://noter.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>jQuery $(document).ready()</title>
		<link>https://noter.tw/2301/jquery-document-ready/</link>
					<comments>https://noter.tw/2301/jquery-document-ready/#respond</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Tue, 25 Sep 2012 14:05:22 +0000</pubDate>
				<category><![CDATA[網頁前端]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[$.ready()]]></category>
		<category><![CDATA[$(document).ready()]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">http://wazai.net/?p=2301</guid>

					<description><![CDATA[<p>前陣子跟新同事聊天的時候聊到，$(document).ready(function(){}});的問題，有同事就舉了個例子來說明，其實小蛙沒有真正去研究過為什麼要寫這個，只知道「寫了比較保險」，聽過例&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/2301/jquery-document-ready/" data-wpel-link="internal">jQuery $(document).ready()</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>前陣子跟新同事聊天的時候聊到，$(document).ready(function(){}});的問題，有同事就舉了個例子來說明，其實小蛙沒有真正去研究過為什麼要寫這個，只知道「寫了比較保險」，聽過例子之後恍然大悟，原來有這麼一層原因在。 </p>



<span id="more-2301"></span>



<pre class="left red">$(document).ready(function(){}});</pre>



用最最白話的方式來說，就是說<mark class="pink-solid">當整個HTML元素都被載入之後，才開始做以下動作</mark>。咦？還是覺得不重要？同事舉了一個簡單的例子



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">&lt;html&gt;
&nbsp;&nbsp;&lt;head&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;script type="text/javascript" src="jquery-1.8.2.min.js"&gt;&lt;/script&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;script type="text/javascript"&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert($('#mydiv').text());
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/script&gt;
&nbsp;&nbsp;&lt;/head&gt;
&nbsp;&nbsp;&lt;body&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;div id="mydiv"&gt;mydiv&lt;/div&gt;
&nbsp;&nbsp;&lt;/body&gt;
&lt;/html&gt;</pre>



<p>上面的例子執行後會發現，alert出來空白的東西，並不符合我們的預期，預期應該要印出mydiv的字樣。如果把alert包在$(function(){}});裡面試試 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">&lt;html&gt;
&nbsp;&nbsp;&lt;head&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;script type="text/javascript" src="jquery-1.8.2.min.js"&gt;&lt;/script&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;script type="text/javascript"&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(function(){alert($('#mydiv').text());});
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/script&gt;
&nbsp;&nbsp;&lt;/head&gt;
&nbsp;&nbsp;&lt;body&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;div id="mydiv"&gt;mydiv&lt;/div&gt;
&nbsp;&nbsp;&lt;/body&gt;
&lt;/html&gt;</pre>



<p>就如同預期的印出 mydiv 字樣了！會造成這個結果的原因是，head部分的JS程式碼在整個網頁還沒完整載入前就已經先執行，因此找不到 mydiv 這個 DOM 元素，而將alert程式碼放在 $(function(){}); 之中，等到整個網頁元素載入完成後才執行，自然就可以找到 mydiv 這個元素而印出當中的值了。其實這部分應該算是JavaScript基礎班，小蛙真是覺得不太好意思。 </p>



<p>附帶一提，小蛙為什麼有時候忘記加$(function(){});也還是可以正常執行呢？是因為小蛙習慣把JavaScript寫在網頁的最後，如果沒有&#8221;意外&#8221;的話，會是可以正常執行的(寫$(function(){});是好習慣！記下來吧！)。例如： </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">&lt;html&gt;
&nbsp;&nbsp;&lt;head&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;script type="text/javascript" src="jquery-1.8.2.min.js"&gt;&lt;/script&gt;
&nbsp;&nbsp;&lt;/head&gt;
&nbsp;&nbsp;&lt;body&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;div id="mydiv"&gt;mydiv&lt;/div&gt;
&nbsp;&nbsp;&lt;/body&gt;
&lt;/html&gt;
&lt;script type="text/javascript"&gt;
&nbsp;&nbsp;alert($('#mydiv').text());
&lt;/script&gt;</pre>



<p>另外在 <strong><a rel="noreferrer noopener nofollow external" aria-label=" (在新分頁中開啟)" href="http://api.jquery.com/ready/" target="_blank" data-wpel-link="external" class="wpel-icon-right">jQuery官網<span class="wpel-icon wpel-image wpel-icon-6"></span></a></strong> 中提到 </p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p> All three of the following syntaxes are equivalent:<br> <code>$(document).ready(handler)</code><br> <code>$().ready(handler)</code>&nbsp;(this is not recommended)<br> <code>$(handler)</code> </p></blockquote>



<p>也就是說$(document).ready(function(){}); 可以縮寫成 $(function(){}});，這兩個是一樣的意思(既然一樣，為什麼不選短的來用呢 @@?) </p>



<p>更多內容可參考<strong><a rel="noreferrer noopener nofollow external" href="http://api.jquery.com/ready/" target="_blank" data-wpel-link="external" class="wpel-icon-right"> jQuery 官網<span class="wpel-icon wpel-image wpel-icon-6"></span></a></strong> 或 <strong><a rel="noreferrer noopener nofollow external" href="https://groups.google.com/forum/?fromgroups=#!topic/jquery-/Ht6r9hTsCVs" target="_blank" data-wpel-link="external" class="wpel-icon-right">此討論串<span class="wpel-icon wpel-image wpel-icon-6"></span></a></strong>。 </p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/2301/jquery-document-ready/" data-wpel-link="internal">jQuery $(document).ready()</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/2301/jquery-document-ready/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
