hover effect for li list

2019-08-28 23:25发布

问题:

I have an <ol>list to which I have added color. This is working fine. But when I add a div, which has 2 tags the hover effect is not applying.

In my code, the hover is not happening for second <li>.

HTML:

<div class="test">
  <ol>
    <a href="#"><li><div><span>test 1</span><p>Mvdshe</p></div></li></a>
    <a href="#"><li><div><span>test 12</span><p>sdgmksgb</p></div>
    <div class="icons"><a href="#" class="play"></a><span class="dividr"></span><a href="#" class="suffle"></a></div></li></a>
    <a href="#"><li><div><span>test 11</span><p>Name dgdg</p></div></li></a>
  </ol>
</div>

CSS:

.test{ position:relative; height:auto; width:100%}
.test ol{margin:0; padding:0; font:normal 30px 'SegoeWPLight', Verdana, Geneva, sans-serif; color:#afafaf; text-align:left; list-style:none; counter-reset: listCounter;}
.test ol li{vertical-align:middle;  padding:10px 20px; border-bottom:#d4d4d4 solid 1px; border-top:#fff solid 1px; counter-increment: listCounter;}
.testol li:before {  content: counter(listCounter) " "; width:30px; margin-right:15px;    display: inline-block;    text-align: right;   }
.test ol li span{vertical-align:middle; display:block; color:#008fcd; font:normal 18px 'SegoeWPLight', Verdana, Geneva, sans-serif;}
.test ol li p{vertical-align:middle; display:block; margin:0; padding:0; font:normal 14px 'SegoeWPLight', Verdana, Geneva, sans-serif; color:#797979;}
.test ol li img{vertical-align:middle; padding-left:10px }
.test ol li div{ display:inline-block; margin-left:25px; vertical-align:middle; }
.test ol a{ text-decoration:none; font:normal 30px 'SegoeWPLight', Verdana, Geneva, sans-serif; color:#afafaf;}
.test ol a:hover li{ background-color:#f4f4f4}
.test ol a.mus_active li{ background-color:#f4f4f4}
.icons{display:inline-block; float:right; vertical-align:middle; width:auto;  margin-top:20px}
.play{background:url(http://www.timeshighereducation.co.uk/magazine/graphics/search_icon_big.gif) no-repeat left; width:25px; height:25px; background-size:15px; display:inline-block !important;  }
.suffle{background:url(../images/suffle.png) no-repeat left;  width:25px; height:25px; display:inline-block !important}
.dividr{background:url(http://www.timeshighereducation.co.uk/magazine/graphics/search_icon_big.gif) no-repeat left; width:3px; height:28px; display:inline-block !important;  margin:0 8px 10px 0}

↪ View this code at JSFiddle

回答1:

try this one http://jsfiddle.net/bFVGr/2/

.test ol li { vertical-align:middle;  border-bottom:#d4d4d4 solid 1px; border-top:#fff solid 1px; counter-increment: listCounter; }

.test ol li > a{display:block; padding:10px 20px;}

.test ol li > a:hover { background-color:#f4f4f4 }


回答2:

It is wrong to put LI inside anchor tag. Write like this:

<ol>
 <li><a></a></li>
 <li><a></a></li>
</ol>

In FF & chrome your code render like this:

<ol>
<a href="#"><li><div><span>test 1</span><p>Mvdshe</p></div></li></a>
<a href="#"></a><li><a href="#"><div><span>test 12</span><p>sdgmksgb</p></div>
</a><div class="icons"><a href="#"></a><a class="play" href="#"></a><span class="dividr"></span><a class="suffle" href="#"></a></div></li>
<a href="#"><li><div><span>test 11</span><p>Name dgdg</p></div></li></a>

</ol>


回答3:

Provided you don't wanna change your list structure as suggested by others one silly thing you could do is put the second list inside a div and apply the same effect ;)

<div id = "special">
<a href="#">
 <li>
    <div>
        <span>test 12</span><p>sdgmksgb</p>
    </div>

    <div class="icons">
        <a href="#" class="play"></a><span class="dividr"></span>
        <a href="#"class="suffle"></a>
    </div>
 </li>
</a>
</div>   

and

 .test #special:hover{background-color:#f4f4f4}

see the fiddlecode here

Edit
To make the entire div clickable which I think is what you want, you can do

  .test #special a{display:block}

or see this