2010-08-26 24 views
0
$(function(){ 
    $("#mainContainer #container:last-child").css("background-image","url('/images/content-title.png') no-repeat") 
}) 

上面的代码没有工作,没有什么happend与我的最后一个孩子选择器有任何错误?

$(function(){ 
    $("#mainContainer #container:last-child").css("background-image","url('/images/content-title.png')") 
}) 

以上代码的工作,但它改变所有的#container的背景和重复-Y。

不仅最后孩子

我想是改变#container的背景图像的最后一个孩子和不重复

我的HTML

<div id="mainContainer">//width 930px margin 0 auto 
    <div id="container">//height 500px test background repeat-y 
    dynamic content here 
    </div> 
</div> 
+0

是的jQuery或类似PHP或ASP.NET服务器语言生成动态内容?如果它在服务器端生成,我们可以看到它可能是什么样子? – BoltClock 2010-08-26 17:41:30

+0

没关系,我放弃已经大声笑...是由PHP生成的,我只是在页脚上添加1个div。谢谢你回答我的问题 – user259752 2010-08-26 17:46:06

回答

0

你想这样的:

$("#mainContainer #container > :last-child") 
    .css("background","url('/images/content-title.png') no-repeat"); 
  1. #container:last-child意味着你正在寻找#container这是最后一个子无论是元素坐镇内线,最后一个孩子的#container

    #container > :last-child另一方面是指作为#container元素的最后一个子元素的任何元素。

  2. 为了设置背景图像在同一条线上重复值,则需要使用简写background CSS属性。相反,如果你不想使用速记你可以这样做,而不是:

    $("#mainContainer #container > :last-child") 
        .css({ 
         "background-image": "url('/images/content-title.png')", 
         "background-repeat": "no-repeat" 
        }); 
    
相关问题