How to add a class to a loaded content with jQuery
I am loading a content and I would like to add a class to that content.
Main content
&开发者_StackOverflow社区lt;div id="notice">
</div>
Content to be loaded.
<p>Se vår <br />
siste <span>KAMPANJE</span></p>
<p>Våre <span>TILBUD</span></p>
My jQuery so far.
$('#notice').load('notice.asp'); // this loads ok.
// but this does not
$("#notice p:even").addClass("bluenotice noticecommon");
$("#notice p:odd").addClass("greennotice noticecommon");
Can I addClass() to a loaded content?
Thanks in advance.
You need to execute that in the .load()
callback, since the content is loaded later (when the server responds with data), like this:
$('#notice').load('notice.asp', function() {
$("#notice p:even").addClass("bluenotice noticecommon");
$("#notice p:odd").addClass("greennotice noticecommon");
});
Or a bit faster in older browsers:
$('#notice').load('notice.asp', function() {
$(this).find("p:even").addClass("bluenotice noticecommon");
$(this).find("p:odd").addClass("greennotice noticecommon");
});
精彩评论