2010-12-07 64 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"); 

我可以將AddClass()添加到加載的內容嗎?

在此先感謝。

回答

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 2010-12-07 16:19:23