将类添加到第 1 和第 3 个 div - 然后重置计数并重新开始
我希望有人能提供帮助。
我有一个由 3 个 div 组成的页面(显示存档的新闻通讯),然后是一个clearBoth - 然后是另一行 3 个 div 等。
基本上我想做的 - 而不是每次更新时手动将类分配给第一个和第三个 div带有新新闻通讯的页面(最新的在前) - 是通过 jQuery 分配类的顶部。但是,我希望计数在 ClearBoth 之后重置自身,以确保将类分配给正确的 div。
我知道 jQuery 是基于 0 的,所以我需要一个名为“firstPanel”的类分配给 0 div(显示为第一个),然后将一个“floatRight”类分配给第二个 div(显示为第三个)。
目前我有这个函数...
$(function(){
$('.newsletterPanel:nth-child(1n)').addClass('firstPanel');
$('.newsletterPanel:nth-child(2n)').removeClass('firstPanel');
$('.newsletterPanel:nth-child(3n)').addClass('floatRight');
});
它正在生成以下 HTML...
<div class="newsletterPanel"><a class="panelLink" href="/emailnewsletters/2011-06-29/online.html" target="_blank">View newsletter </a>
<div class="newsletterInfo">
<p>Broadcasted on 29/06/2011</p>
<p class="genericLinkButton"><a href="/emailnewsletters/2011-06-29/online.html" target="_blank" rel="group">View June's newsletter <span class="smallArrow">></span></a></p>
</div>
</div>
<div class="newsletterPanel firstPanel"><a class="panelLink" href="/emailnewsletters/2011-05-25/online.html" target="_blank">View newsletter </a>
<div class="newsletterInfo">
<p>Broadcasted on 25/05/2011</p>
<p class="genericLinkButton"><a href="/emailnewsletters/2011-05-25/online.html" target="_blank" rel="group">View May's newsletter <span class="smallArrow">></span></a></p>
</div>
</div><div class="clearBoth"></div>
<div class="newsletterPanel floatRight"><a class="panelLink" href="/emailnewsletters/2011-04-27/online.html" target="_blank">View newsletter </a>
<div class="newsletterInfo">
<p>Broadcasted on 27/04/2011</p>
<p class="genericLinkButton"><a href="/emailnewsletters/2011-04-27/online.html" target="_blank" rel="group">View April's newsletter <span class="smallArrow">></span></a></p>
</div>
</div>
<div class="newsletterPanel firstPanel"><a class="panelLink" href="/emailnewsletters/2011-03-09/online.html" target="_blank">View newsletter </a>
<div class="newsletterInfo">
<p>Broadcasted on 09/03/2011</p>
<p class="genericLinkButton"><a href="/emailnewsletters/2011-03-09/online.html" target="_blank" rel="group">View March's newsletter <span class="smallArrow">></span></a></p>
</div>
</div>
<div class="newsletterPanel"><a class="panelLink" href="/emailnewsletters/2011-01-27/online.html" target="_blank">View newsletter </a>
<div class="newsletterInfo">
<p>Broadcasted on 27/01/2011</p>
<p class="genericLinkButton"><a href="/emailnewsletters/2011-01-27/online.html" target="_blank" rel="group">View January's newsletter <span class="smallArrow">></span></a></p>
</div>
</div>
</div>
现在的问题是这些类没有应用于正确的 div。
有什么想法吗?
提前致谢
I'm hoping someone can help.
I have a page made up of 3 divs (showing archived newsletters), then a clearBoth - then another row of 3 divs etc.
Basically what i want to do - rather than manually assigning classes to the 1st and 3rd div every time i update the page with a new newsletter (most recent first) - is top assign classes through jQuery. However, i want the count to reset itself after the clearBoth so as to ensure that the classes are assigned to the correct divs.
I understand that jQuery is 0 based, so I would need a class called 'firstPanel' assigned to the 0 div (appears as 1st), and then a class of 'floatRight' assigned to the 2nd (which appears as the 3rd).
Currently I have this function...
$(function(){
$('.newsletterPanel:nth-child(1n)').addClass('firstPanel');
$('.newsletterPanel:nth-child(2n)').removeClass('firstPanel');
$('.newsletterPanel:nth-child(3n)').addClass('floatRight');
});
Which is generating the following HTML...
<div class="newsletterPanel"><a class="panelLink" href="/emailnewsletters/2011-06-29/online.html" target="_blank">View newsletter </a>
<div class="newsletterInfo">
<p>Broadcasted on 29/06/2011</p>
<p class="genericLinkButton"><a href="/emailnewsletters/2011-06-29/online.html" target="_blank" rel="group">View June's newsletter <span class="smallArrow">></span></a></p>
</div>
</div>
<div class="newsletterPanel firstPanel"><a class="panelLink" href="/emailnewsletters/2011-05-25/online.html" target="_blank">View newsletter </a>
<div class="newsletterInfo">
<p>Broadcasted on 25/05/2011</p>
<p class="genericLinkButton"><a href="/emailnewsletters/2011-05-25/online.html" target="_blank" rel="group">View May's newsletter <span class="smallArrow">></span></a></p>
</div>
</div><div class="clearBoth"></div>
<div class="newsletterPanel floatRight"><a class="panelLink" href="/emailnewsletters/2011-04-27/online.html" target="_blank">View newsletter </a>
<div class="newsletterInfo">
<p>Broadcasted on 27/04/2011</p>
<p class="genericLinkButton"><a href="/emailnewsletters/2011-04-27/online.html" target="_blank" rel="group">View April's newsletter <span class="smallArrow">></span></a></p>
</div>
</div>
<div class="newsletterPanel firstPanel"><a class="panelLink" href="/emailnewsletters/2011-03-09/online.html" target="_blank">View newsletter </a>
<div class="newsletterInfo">
<p>Broadcasted on 09/03/2011</p>
<p class="genericLinkButton"><a href="/emailnewsletters/2011-03-09/online.html" target="_blank" rel="group">View March's newsletter <span class="smallArrow">></span></a></p>
</div>
</div>
<div class="newsletterPanel"><a class="panelLink" href="/emailnewsletters/2011-01-27/online.html" target="_blank">View newsletter </a>
<div class="newsletterInfo">
<p>Broadcasted on 27/01/2011</p>
<p class="genericLinkButton"><a href="/emailnewsletters/2011-01-27/online.html" target="_blank" rel="group">View January's newsletter <span class="smallArrow">></span></a></p>
</div>
</div>
</div>
Now the problem is the classes are not being applied to the right divs.
Any ideas?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个
Try this
我不确定我是否理解为什么您不想通过您选择的服务器端语言将类分配给第一个和第三个(我假设这些数据是通过循环服务器端从数据源填充的) ?)然后您可以仅通过 CSS 应用您的样式。 (例如,使用“奇数”或“偶数”类创建元素并具有相应的样式)
这将有一些好处:
作为解决方法的一部分,您是否需要将这些类应用于子元素?
I'm not sure I understand why you wouldn't just want to assign a class to the 1st and 3rd via your server-side language of choice (I'm assuming that this data is being populated from a datasource via a loop server side?) You could then apply your styles through just CSS. (eg. create your elements with the classes "odd" or "even" and have a corresponding style)
This would have a few benefits:
Do you need to apply the classes to the child elements as part of a work-around?
使用这些选择器:
如果您需要可重复的组,每个组包含 3 个元素。
Use these selectors:
if you need repeatable groups of 3 elements each.