1

我想要一个 div 内的备用背景颜色。我通过下面的代码完成了:

我希望我的第一行以 bgcolor 呈现,而下一行以另一种颜色呈现。

我试过了,但没有用。这是代码:

<script type="text/javascript">
    $(document).ready(function() {
        $(".rowBg div:odd").addClass("altBg");
    });
</script>

/*CSS*/
div.rowBg div { clear:both; padding:0; overflow:hidden }
div.altBg {  background-color:#eee !important;  overflow:hidden; padding:5px 0 } 
<!--HTML-->
<div class="rowBg">
            <div>
            <!--Row1-->
            <span class="alphabets">A</span>
            <span class="itemNames">
            <ul>
            <li><a href="../locate/refine.html?N=4294963558">Acer America</a></li>
            <li><a href="../promotions/index.html?page_id=3703">Acronis</a></li>
            <li><a href="../locate/refine.html?N=4294966145">Adaptec</a></li>
            <li><a href="../promotions/index.html?page_id=3163">Adobe</a></li>
            <li><a href="../promotions/index.html?page_id=4402">AirWatch</a></li>
            </ul>           
            </span>
            </div>
            <!--Row 2-->
            <div>
            <!--Row1-->
            <span class="alphabets">A</span>
            <span class="itemNames">
            <ul>
            <li><a href="../locate/refine.html?N=4294963558">Acer America</a></li>
            <li><a href="../promotions/index.html?page_id=3703">Acronis</a></li>
            <li><a href="../locate/refine.html?N=4294966145">Adaptec</a></li>
            <li><a href="../promotions/index.html?page_id=3163">Adobe</a></li>
            <li><a href="../promotions/index.html?page_id=4402">AirWatch</a></li>

            </ul>           
            </span>
            </div>

            </div>

谢谢,

4

2 回答 2

1

我已经测试了你发布的 HTML/jquery,它正在做你所期望的。每个奇数 Div 都有一个 altBG 类。

我的建议是您的 CSS 错误,但由于尚未发布,我无法确认。如果您将 jquery 的这一行更改为:

$(".rowBg div:odd").addClass("altBg");

$(".rowBg div:odd").css({"background-color":"#ff0000"})

您应该能够看到您的标记和 jquery 没问题。

希望这可以帮助。

于 2013-08-06T15:46:20.030 回答
0

你确定你已经添加了 jquery 库吗?因为您的 HTML、CSS 和 jQuery 代码很好,但您需要添加库并且您的代码可以正常工作。你可以从这里下载 jQuery 库 jQuery

<style type="text/css">
div.rowBg div { clear:both; padding:0; overflow:hidden }
div.altBg {  background-color:#eee !important;  overflow:hidden; padding:5px 0 } 
</style>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $(".rowBg div:odd").addClass("altBg");
    });
</script>

<div class="rowBg">
            <div>
            <!--Row1-->
            <span class="alphabets">A</span>
            <span class="itemNames">
            <ul>
            <li><a href="../locate/refine.html?N=4294963558">Acer America</a></li>
            <li><a href="../promotions/index.html?page_id=3703">Acronis</a></li>
            <li><a href="../locate/refine.html?N=4294966145">Adaptec</a></li>
            <li><a href="../promotions/index.html?page_id=3163">Adobe</a></li>
            <li><a href="../promotions/index.html?page_id=4402">AirWatch</a></li>
            </ul>           
            </span>
            </div>
            <!--Row 2-->
            <div>
            <!--Row1-->
            <span class="alphabets">A</span>
            <span class="itemNames">
            <ul>
            <li><a href="../locate/refine.html?N=4294963558">Acer America</a></li>
            <li><a href="../promotions/index.html?page_id=3703">Acronis</a></li>
            <li><a href="../locate/refine.html?N=4294966145">Adaptec</a></li>
            <li><a href="../promotions/index.html?page_id=3163">Adobe</a></li>
            <li><a href="../promotions/index.html?page_id=4402">AirWatch</a></li>

            </ul>           
            </span>
            </div>
<div>
            <!--Row1-->
            <span class="alphabets">A</span>
            <span class="itemNames">
            <ul>
            <li><a href="../locate/refine.html?N=4294963558">Acer America</a></li>
            <li><a href="../promotions/index.html?page_id=3703">Acronis</a></li>
            <li><a href="../locate/refine.html?N=4294966145">Adaptec</a></li>
            <li><a href="../promotions/index.html?page_id=3163">Adobe</a></li>
            <li><a href="../promotions/index.html?page_id=4402">AirWatch</a></li>
            </ul>           
            </span>
            </div>
<div>
            <!--Row1-->
            <span class="alphabets">A</span>
            <span class="itemNames">
            <ul>
            <li><a href="../locate/refine.html?N=4294963558">Acer America</a></li>
            <li><a href="../promotions/index.html?page_id=3703">Acronis</a></li>
            <li><a href="../locate/refine.html?N=4294966145">Adaptec</a></li>
            <li><a href="../promotions/index.html?page_id=3163">Adobe</a></li>
            <li><a href="../promotions/index.html?page_id=4402">AirWatch</a></li>
            </ul>           
            </span>
            </div>
<div>
            <!--Row1-->
            <span class="alphabets">A</span>
            <span class="itemNames">
            <ul>
            <li><a href="../locate/refine.html?N=4294963558">Acer America</a></li>
            <li><a href="../promotions/index.html?page_id=3703">Acronis</a></li>
            <li><a href="../locate/refine.html?N=4294966145">Adaptec</a></li>
            <li><a href="../promotions/index.html?page_id=3163">Adobe</a></li>
            <li><a href="../promotions/index.html?page_id=4402">AirWatch</a></li>
            </ul>           
            </span>
            </div>

            </div>
于 2013-08-06T18:27:32.690 回答