2012-12-07 31 views
0

如果我把我的jQuery放在沒有$(document).ready(function()的body的末尾,它可以正常工作,如果我使用$(document ).ready(function(),它不再起作用,我意識到當我將jQuery放置在頭部時,我需要function(),但是當我嘗試自己實現它時,沒有骰子,我盯着它太長時間,我沒有看到這個問題。jQuery不在頭部,但在身體確定

<head> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
     $("#testTable tr").hover(
      function(){$(this).addClass("highlighted");}, 
      function(){$(this).removeClass("highlighted");} 
     ); 
}); 
</script> 

<style type='text/css'> 
.tablerow {background-color:yellow;} 
.tableheader {background-color:Pink;} 
.highlighted {background-color:Green;} 
</style>  
</head> 

<body> 
<table id="testTable"> 
    <thead class="tableheader"> 
     <tr><th>No</th><th>Name</th><th>Age</th><th>Salary</th></tr> 
    </thead> 
    <tbody id="testBody"> 
     <tr class="tablerow"><td>1</td><td>Yong Mook Kim</td><td>28</td><td>$100,000</td></tr> 
     <tr class="tablerow"><td>2</td><td>Low Yin Fong</td><td>29</td><td>$90,000</td></tr> 
    </tbody> 
</table> 
</body> 
+1

,準確的HTML似乎爲我 –

+0

工作什麼是「不工作」?文檔是什麼樣e你會得到什麼錯誤? – kennypu

+0

我假設你在你的標記中有''標記? – VisioN

回答

-1

嘗試腳本之前引用的css文件,這裏是http://api.jquery.com/ready/

+0

他的代碼中沒有引用CSS文檔。即使有,它會在'.ready()'運行時加載,所以順序不應該緊。 – Barmar

+0

我的意思是在腳本標記之前放置style標記,從文檔中提到:「當使用依賴於CSS樣式屬性值的腳本時,在引用腳本之前引用外部樣式表或嵌入樣式元素很重要。」 – user1801279

+0

這句話解釋了爲什麼你應該使用'.ready()'處理程序 - 它確保你滿足這個要求。 – Barmar

相關問題