2014-05-09 35 views
0

我使用的是Mac OS 10.6,Chrome 34和jQuery 2.1.1。 我有這個從TutsPlus'30天學習jQuery'的簡單代碼。 我聲稱有jQuery中的錯誤。 或...你告訴我! ;)Jquery - > Document.Ready - >不會觸發

這不起作用:

<html> 
<head> 
    <title> Does not work </title> 
    <script type="text/javascript" src="jquery-2.1.1.js"></script> //script is in root 
    <style> 
    .emphasis{font-weight: bold;} 
    </style> 

    <script> 
    $(document).ready(function()){$('li:first-child').addClass('emphasis');}); 
    </script> 

</head> 
<body> 
    <ul> 
    <li>Hello</li> 
    <li>Hello 2</li> 
    <li>Hello 3</li> 
    </ul> 
</body> 
</html> 

這工作:

<html> 
<head> 
    <title> </title> 
    <script type="text/javascript" src="jquery-2.1.1.js"></script> 
    <style> 
    .emphasis{font-weight: bold;} 
    </style> 


    </head> 
<body> 

<ul> 
    <li>Hello</li> 
    <li>Hello 2</li> 
    <li>Hello 3</li> 
</ul> 

    <script> 
    $('li:first-child').addClass('emphasis'); 
    </script> 

</body> 
</html> 
+1

語法錯誤'$(文件)。就緒(函數(){ $( '李:第一胎')。addClass( '重點'); });' - 額外')'在'function())' –

+0

嗨,謝謝你的答案。我可以問你一下,你能推薦一個有語法檢查的編輯嗎? – InnerOrchestra

+0

有很多編輯... Dreamviewer可能是我認爲...或者你有aptana –

回答

2

嘗試刪除額外括號,

$(document).ready(function()){$('li:first-child').addClass('emphasis');}); 
//--------------------------^ 

有效代碼:

$(document).ready(function() { 
    $('li:first-child').addClass('emphasis'); 
}); 
+0

謝謝Raja! – InnerOrchestra

2

您錯誤地關閉了文檔就緒功能。您關閉額外的一個封閉的大括號就是爲什麼它不會工作

$(document).ready(function() { 
      $('li:first-child').addClass('emphasis'); 
}); 
+0

謝謝你sudharsan! – InnerOrchestra

1

更改以下行

<script> 
    $(document).ready(function()){$('li:first-child').addClass('emphasis');}); 
</script> 

<script> 
    $(document).ready(function(){ 
     $('li:first-child').addClass('emphasis'); 
    }); 
</script> 
+0

謝謝Bhushan! – InnerOrchestra

0

你必須(如你function()後得到了一個額外的))刪除附加添加的那個左括號。

$(document).ready(function(){ 
$('li:first-child').addClass('emphasis'); 
});