仅识别第一个第n个子命令

时间:2013-01-27 00:05:02

标签: html css css-selectors

我正在尝试修改此“悬停在除了”之外的任何代码(http://csstricks.com/examples/HoverEverythingBut/),以便悬停在上面的链接会改变颜色,或者最终可能会被图像替换。我认为实现这一目标的最佳方法是使用nth-child命令,我添加了以下CSS ...

    #all:hover ul li a:hover:nth-child(1)  { background: #ffffff no-repeat; }
    #all:hover ul li a:hover:nth-child(2)  { background: #000000 no-repeat; }
    #all:hover ul li a:hover:nth-child(3)  { background: #bbbbbb no-repeat; }
    #all:hover ul li a:hover:nth-child(4)  { background: #c73b1b no-repeat; }
    #all:hover ul li a:hover:nth-child(5)  { background: #367db2 no-repeat; }
    #all:hover ul li a:hover:nth-child(6)  { background: #111111 no-repeat; }
    #all:hover ul li a:hover:nth-child(7)  { background: #222222 no-repeat; }
    #all:hover ul li a:hover:nth-child(8)  { background: #333333 no-repeat; }
    #all:hover ul li a:hover:nth-child(9)  { background: #444444 no-repeat; }

不幸的是,似乎只识别了第一个第n个子命令。现在,只要我将鼠标悬停在链接上(无论是第一个还是最后一个),背景都将变为白色。

任何帮助将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:1)

如果您的html与<ul><li><a></li><li><a></li>...类似,那么第n个孩子应该在li

#all:hover ul li:nth-child(1) a:hover  { background: #ffffff no-repeat; }
#all:hover ul li:nth-child(2) a:hover  { background: #000000 no-repeat; }
#all:hover ul li:nth-child(3) a:hover  { background: #bbbbbb no-repeat; }
#all:hover ul li:nth-child(4) a:hover  { background: #c73b1b no-repeat; }
#all:hover ul li:nth-child(5) a:hover  { background: #367db2 no-repeat; }
#all:hover ul li:nth-child(6) a:hover  { background: #111111 no-repeat; }
#all:hover ul li:nth-child(7) a:hover  { background: #222222 no-repeat; }
#all:hover ul li:nth-child(8) a:hover  { background: #333333 no-repeat; }
#all:hover ul li:nth-child(9) a:hover  { background: #444444 no-repeat; }