2014-05-09 3 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

구문 오류'$ (문서) .ready (함수() { $ ('리 : 첫 아이'). 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

라자 감사합니다! – InnerOrchestra

2

실수로 문서 준비 기능을 실수로 닫습니다. 당신이이

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

sudharsan 감사합니다! – InnerOrchestra

1

변경을 작동하지 않습니다 이유 중괄호를 폐쇄 추가로 추가 된 닫는 괄호를 제거하십시오.

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

감사합니다 Bhushan! – InnerOrchestra

0

(당신이 function() 후 1 추가 )있어로) 당신은 여분의 하나는에 라인

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

아래

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