2010-12-07 5 views
1

컨텐츠를로드 중이므로 해당 컨텐츠에 클래스를 추가하고 싶습니다.jQuery를 사용하여로드 된 컨텐츠에 클래스를 추가하는 방법

주요 내용

<div id="notice"> 

</div> 

컨텐츠를로드 할 수 있습니다.

<p>Se v&aring;r <br /> 
siste <span>KAMPANJE</span></p> 
<p>V&aring;re <span>TILBUD</span></p> 

지금까지 내 jQuery.

$('#notice').load('notice.asp'); // this loads ok. 

// but this does not 
$("#notice p:even").addClass("bluenotice noticecommon"); 

$("#notice p:odd").addClass("greennotice noticecommon"); 

로드 된 컨텐츠에 classClass()를 추가 할 수 있습니까?

미리 감사드립니다. (서버에 데이터가 응답 할 때) 내용이 나중에로드되기 때문에

답변

6

당신은 다음과 같이의 .load() 콜백이 실행해야합니다

$('#notice').load('notice.asp', function() { 
    $("#notice p:even").addClass("bluenotice noticecommon"); 
    $("#notice p:odd").addClass("greennotice noticecommon"); 
}); 

또는 조금 더 빠른에서 이전 버전의 브라우저 :

$('#notice').load('notice.asp', function() { 
    $(this).find("p:even").addClass("bluenotice noticecommon"); 
    $(this).find("p:odd").addClass("greennotice noticecommon"); 
}); 
+0

당신은 저를 때려 눕 힙니다.) +1 – jwueller

관련 문제