动态附加div和类

时间:2015-01-17 16:47:55

标签: javascript jquery css asp.net-mvc-4 razor

我正在尝试在mvc4 razor中安排新闻项目,重复列表,将每两篇文章包含在div类“news-group clearfix”中,并将css元素“last”添加到第二篇文章类中,如下所示。

<div id="main-content">

  <div class="news-group clearfix">
     <article class="news static-page">...</article>
     <article class="news static-page last">...</article>
     </div>

    **<div class="news-group clearfix">**
      <article class="news-container news static-page">...</article>
     <article class="news-container static-page **last**">...</article>
     **</div>**

    **<div class="news-group clearfix">**
      <article class="news-container news static-page">...</article>
     <article class="news-container static-page **last**">...</article>
     **</div>**
</div>

我已经能够使用下面的脚本添加“last”

<script type="text/javascript">
$(document).ready(function () {
$('article.news-container:even').addClass('last');
});
</script> 

我需要帮助,了解如何在剃刀视图中使用<div class=news-group clearfix><div>封装2篇文章的重复单元。任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

你可能会做这样的事情

$('article.news-container:odd').each(function(){
       $(this).next().addBack().wrapAll("<div class="news-group clearfix"></div>");
});

答案 1 :(得分:0)

<强> JQUERY

$('.news-group.clearfix').append('<div>hello</div>').addClass('last');

**OR**

    $('.news-group.clearfix').addClass('last').append('<div>hello</div>');