[英]How can I make a button keep the focused css style till I click on it again
I basically want the button to stay with the hover css style regardless of every other activity instead of going back to the normal style. 我基本上希望按钮保持悬停css样式而不管其他所有活动,而不是回到正常样式。 Please help out! 请帮忙!
$(function() { var clickCount = 0; $('.menu').click(function() { if (clickCount % 2 == 0) { //do when open $('#css-menu').css({right: '0'}); }else{ //do when closed $('css-menu').css({right: '-340px'}); } clickCount++; }); });
.main-nav ul li.menu { } .main-nav ul li.menu:after { } .main-nav ul li.menu:hover:after { }
<li class='menu' title='Menu'></li>
As it was mentionned in the comments, you should work with classes instead of a counter and the :hover selector. 正如在评论中提到的那样,您应该使用类而不是计数器和:hover选择器。 Here is what I think you should do : 这是我认为你应该做的:
$(function() {
$('.menu').click(function() {
if ($(this).hasClass("hover")) {
$(this).removeClass("hover");
}else{
$(this).addClass("hover");
}
});
Then in your CSS, create a style with .menu .hover : 然后在CSS中,使用.menu .hover创建一个样式:
.main-nav ul li.menu .hover {}
If you wanted to stay hovered color using CSS
then you need to use :visited
:visited
is a pseudo-class selector that can change some of the styling on an anchor link (a)
. 如果你想使用CSS
保持悬停的颜色,那么你需要使用:visited
:visited
是一个伪类选择器,它可以改变锚链接上的一些样式(a)
。 So you can't use for li
. 所以你不能用于li
。
but you can use like this .main-nav ul li.menu a {}
and .main-nav ul li.menu a:hover, .main-nav ul li.menu a:visited, .main-nav ul li.menu a:active {}
但你可以像这样使用.main-nav ul li.menu a {}
和.main-nav ul li.menu a:hover, .main-nav ul li.menu a:visited, .main-nav ul li.menu a:active {}
jsfiddle: https://jsfiddle.net/L1utc1o8/11/ jsfiddle: https ://jsfiddle.net/L1utc1o8/11/
This solution uses Jquery to manually add and remove a class from a button witch the CSS then picks up. 这个解决方案使用Jquery手动添加一个类,然后从CSS中取出一个类。 Is this what you wanted? 这是你想要的吗?
html HTML
<button id="button" onclick="myFunction()"> select </button>
js JS
function myFunction() {
if($( "#button" ).hasClass( "hover" )){
$( "#button" ).removeClass( "hover" )
}
else{
$( "#button" ).addClass( "hover" );
}
}
css CSS
.hover{
background-color: #4CAF50; /* Green */
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
} }
Happy Coding :) 快乐编码:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.