2014-05-09 14 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'); 
});