admin管理员组文章数量:1253367
I have a ul
with list items that are highlighted when they are hovered over or focused upon. However if there is one being hovered and another being focused, then I only want the hovered li
to change style and the focused elements style to go back to default. Is this possible? Here is a demo. Thank you.
css
li:hover,li:focus{
background-color:yellow;
cursor:pointer;
}
html
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
js
$('li').attr('tabindex','0').focus();
I have a ul
with list items that are highlighted when they are hovered over or focused upon. However if there is one being hovered and another being focused, then I only want the hovered li
to change style and the focused elements style to go back to default. Is this possible? Here is a demo. Thank you.
css
li:hover,li:focus{
background-color:yellow;
cursor:pointer;
}
html
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
js
$('li').attr('tabindex','0').focus();
Share
Improve this question
edited Feb 6, 2015 at 15:58
talemyn
7,9604 gold badges32 silver badges52 bronze badges
asked Feb 6, 2015 at 15:51
user2014429user2014429
2,57710 gold badges38 silver badges52 bronze badges
3
- Since you have tagged JavaScript and jQuery, I assume you are not looking for a pure CSS solution. Is that a valid assumption? – Harry Commented Feb 6, 2015 at 16:00
- Remove li:focus within the CSS? – James George Dunn Commented Feb 6, 2015 at 16:01
- @Harry does not have to be pure css, but if possible, then that would be good. – user2014429 Commented Feb 6, 2015 at 16:02
4 Answers
Reset to default 11Using the code you have, do this ...
li:focus{
background-color:yellow;
cursor:pointer;
outline: none;
}
ul:hover > li:focus {
background: none;
cursor: default;
}
ul:hover > li:hover {
background: yellow;
cursor: pointer;
}
What's the CSS saying?
- Set any list item that has :focus to have yellow background.
- When you hover over the parent UL, set all the li's to have no background.
- When you hover over an individual LI, set the background to yellow.
- When you hover out of the UL, the styling resets.
JS Fiddle
Place your :hover CSS class before :focus...
.textbox:hover
{
border-color: lightskyblue;
}
.textbox:focus
{
border-color: yellowgreen;
}
With Jquery you can use blur() event:
$('li').attr('tabindex', '0').focus()
$('li').hover(function() {
$(this).siblings('li').blur();
})
li:hover,
li:focus {
background-color: yellow;
cursor: pointer;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>hover over one and two and three should not be yellow anymore</p>
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
You could use only the focus style and add a mouseover event on list items to make them focus when hovering. That way, even if you take the mouse out of the list item, it will remain focused.
$('li').mouseover(function() {
$(this).focus();
});
本文标签: javascriptcombine hover and focus selectors styles but not simultaniouslyStack Overflow
版权声明:本文标题:javascript - combine hover and focus selectors styles but not simultaniously - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740762362a2282188.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论