开发者

How to add a style using jQuery to the 3rd, 6th, etc. (n * 3) elements within a div?

开发者 https://www.devze.com 2023-03-20 13:44 出处:网络
I\'m designing a website w开发者_如何转开发ith divs with the following fashion: <div id=\"showcase\">

I'm designing a website w开发者_如何转开发ith divs with the following fashion:

<div id="showcase">
 <div class="project"> <!-- this is the first row -->
 <div class="project"> <!-- this is the first row -->
 <div class="project"> <!-- this is the first row -->
 <div class="project"> <!-- this is the second row -->
 <div class="project"> <!-- this is the second row -->
 <div class="project"> <!-- this is the second row -->
 <div class="project"> <!-- this is the third row -->
 <div class="project"> <!-- this is the third row -->
 <div class="project"> <!-- this is the third row -->
 </div> 
</div>

I want to remove right margin to the "last" div of a row (each row has 3 divs).

Any suggestions to accomplish this?


I think you want the nth-child selector:

$('#showcase .project:nth-child(3n)').addClass('margin-adjuster');

Here's a quick demo that just does a color change to show you how it works:

http://jsfiddle.net/ambiguous/YDvGw/

You could do a .css({ marginRight: 0 }) if you just wanted to change the margin without messing around with another class.

0

精彩评论

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