开发者

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&aring;r <br />
siste <span>KAMPANJE</span></p>
<p>V&aring;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");
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜