将类添加到第 1 和第 3 个 div - 然后重置计数并重新开始

发布于 2024-11-27 03:33:50 字数 3451 浏览 1 评论 0原文

我希望有人能提供帮助。

我有一个由 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">&gt;</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">&gt;</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">&gt;</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">&gt;</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">&gt;</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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷血 2024-12-04 03:33:50

试试这个

$(function(){
   var $this, count = 0, className;
   $('div.newsletterPanel').each(function(i){

       $this = $(this);
       if($this.next().is("div.clearBoth")){
          count = 0;
          $this.addClass("floatRight");
       }
       else{
        if(count == 0)
          $this.addClass("firstPanel");
        else if(count == 1)
          $this.removeClass("firstPanel");

        count++;
       }


   });
});

Try this

$(function(){
   var $this, count = 0, className;
   $('div.newsletterPanel').each(function(i){

       $this = $(this);
       if($this.next().is("div.clearBoth")){
          count = 0;
          $this.addClass("floatRight");
       }
       else{
        if(count == 0)
          $this.addClass("firstPanel");
        else if(count == 1)
          $this.removeClass("firstPanel");

        count++;
       }


   });
});
素罗衫 2024-12-04 03:33:50

我不确定我是否理解为什么您不想通过您选择的服务器端语言将类分配给第一个和第三个(我假设这些数据是通过循环服务器端从数据源填充的) ?)然后您可以仅通过 CSS 应用您的样式。 (例如,使用“奇数”或“偶数”类创建元素并具有相应的样式)

这将有一些好处:

  1. 代码清洁度:这会将客户端表示代码与服务器端内容代码分开。
  2. 可维护性:如果您想更改 CSS 样式,则比重构 JS 更容易(例如,如果您想在以后添加更多 div 该怎么办?)
  3. 保持简单!

作为解决方法的一部分,您是否需要将这些类应用于子元素?

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:

  1. Code cleanliness: This would separate client-side presentation code from server-side content code.
  2. Maintainability: It's much easier to just change your CSS style rather than have to refactor your JS should you want to change it (for example, what if you should want to add more divs down the road?)
  3. Keep it simple!

Do you need to apply the classes to the child elements as part of a work-around?

屌丝范 2024-12-04 03:33:50

使用这些选择器:

newsletterPanel:nth-child(3n+1)
newsletterPanel:nth-child(3n+2)
newsletterPanel:nth-child(3n+3)

如果您需要可重复的组,每个组包含 3 个元素。

Use these selectors:

newsletterPanel:nth-child(3n+1)
newsletterPanel:nth-child(3n+2)
newsletterPanel:nth-child(3n+3)

if you need repeatable groups of 3 elements each.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文