CSS Change color of “:after” element on hover

 ̄綄美尐妖づ 提交于 2019-12-30 05:46:07

问题


Lets say i have a list of elements :

<ol>
    <li>First</li>
    <li>Second</li>
    <li>Third</li>
</ol>

When i hover a list item , i need to be able to get more info about each subject. I did this with CSS :after pseudo-element .

ol {
    list-style-type: decimal-leading-zero;
}

li:hover:after {
    content: " + More info";
}

Here is a jSfiddle

Now when i hover on the More info text , i want to change it's color . I don't really get it how to acheive this effect . i have tried the following css rules , but it does not work as expected .

li:after:hover {
    color: red;
}

li:hover:after:hover {
    color: red;
}

Is it possible to do in CSS ?

If yes , another question is interesting , can i attach an onclick event to the :after element ?


回答1:


As per ExtPro's comment, you can't

But you can do like...

HTML :

<ol>
    <li>First<span> + More info</span></li>
    <li>Second<span> + More info</span></li>
    <li>Third<span> + More info</span></li>
</ol>

CSS :

li > span {
    display:none;
}

li:hover > span {
    display:inline;
}

li > span:hover {
    color:red;
}

Use :active as onClick ?

li > span:active {
    color:blue;
}

Demo : http://jsfiddle.net/79Lvn/2/


or JS/jQuery way ? just...

$('ol > li').append('<span> + More info</span>');

Demo : http://jsfiddle.net/79Lvn/4/




回答2:


this works perfectly

.topButton:hover:after{
    background-color: #000;
}



回答3:


Her you go mate, did a bit of "fiddling" and got it working with CSS using

.third:hover .sub{
      display:inline;   
}

:

http://jsfiddle.net/79Lvn/3/



来源:https://stackoverflow.com/questions/19952986/css-change-color-of-after-element-on-hover

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!