我怎么能为奇数和偶数div标记应用单独的样式呢



我正在使用剃刀视图开发asp.net mvc 4。我试图显示样式为float:left的奇数div标记和样式为float:right的偶数div标记。这里我的div标签是这样的,

    <div id="content" style="padding-top:80px;">
    @foreach (var item in (List<System.Data.DataRow>)ViewBag.List)
    {
        <a href="@string.Format("Products?id={0}",item[0])">
        <div class="img">
            <table align="center" cellspacing="12">
                <tr>
                    <td style="text-align: center;">
                        <b>@item[1]</b>
                    </td>
                </tr>
                <tr>
                    <td style="text-align: center; color: DodgerBlue; font-size: xx-large; border-top: 1px solid Gray;
                        border-bottom: 1px solid gray; font-weight: bolder;">
                        $@item[2]
                    </td>
                </tr>
                <tr>
                    <td style="text-align: center">
                        @item[3]
                    </td>
                </tr>
                <tr>
                    <td style="text-align: center">
                        No Trail period
                    </td>
                </tr>
                <tr>
                <td>
                <div class="divOuter" style="float:left;margin:4px;width:100%;height:40px;text-align:left">
                 @foreach (var i in ProductOption.ProductOptions(Convert.ToInt32(item[0])))
                 {
                     <div class="divInner" style="display:inline;width:50%;"><input type="checkbox" value="@i[0]" checked="checked" /><span style="line-height:20px;font-weight:bold;">@i[0]</span></div>
                 }
                 </div>
                </td>
                </tr>
            </table>
        </div></a>
    }
</div>

我使用过jquery,比如

 $(document).ready(function () {
           $("div.divOuter div.divInner:odd").css("float","left");
           $("div.divOuter div.divInner:even").css("float", "right");
       });

如何将float:left应用于奇数div标记,将float:right应用于偶数div标记。请引导我。

不要设置内部div的id,而是将其class设置为divInner。然后将选择器更改为如下所示:

 $(document).ready(function () {
       $("div.divOuter .divInner:odd").css("float","left");
       $("div.divOuter .divInner:even").css("float", "right");
   });

ID在HTML文档中应该是唯一的。

您缺少了很大一部分-不能有多个id具有相同的名称。你可以使用一个类,然后使用一个选择器,它就会起作用。

请参见此处:http://jsfiddle.net/FMp35/3/

最新更新