have any mistake with my last-child selector?
$(functi开发者_StackOverflow社区on(){
$("#mainContainer #container:last-child").css("background-image","url('/images/content-title.png') no-repeat")
})
code above didn't work , nothing happend
$(function(){
$("#mainContainer #container:last-child").css("background-image","url('/images/content-title.png')")
})
code above work , but it change all the #container background and repeat-y.
not only the last-child
what i want is change the last-child of #container background image and no-repeat
my html
<div id="mainContainer">//width 930px margin 0 auto
<div id="container">//height 500px test background repeat-y
dynamic content here
</div>
</div>
You want this:
$("#mainContainer #container > :last-child")
.css("background","url('/images/content-title.png') no-repeat");
#container:last-child
means you're looking for#container
which is the last child of whatever that element sits inside, not the last child of#container
.#container > :last-child
, on the other hand, refers to any element which is the last child of the#container
element.In order to set both a background image and a repeat value in the same line, you need to use the shorthand
background
CSS property. Conversely, if you don't want to use a shorthand you can do this instead:$("#mainContainer #container > :last-child") .css({ "background-image": "url('/images/content-title.png')", "background-repeat": "no-repeat" });
精彩评论