2010-12-07 108 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