简体   繁体   English

我怎样才能制作一个按钮保持聚焦的CSS样式,直到我再次点击它

[英]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.

相关问题 如何在 html 中制作一个一直按住直到我再次按下它的按钮? - How to make a button in html that keep pressed till i press it again? 如何使单击的按钮一直亮起,直到单击另一个按钮? - How I keep clicked button light up till I click another button? 当我单击制作排序(ab)数组然后再次单击排序(ba)时,如何制作单击按钮 - How can i make a click button when do i click make sort(a-b) array and then click again to sort(b-a) 即使单击外部的按钮,如何保持输入的焦点 - How do I keep an input focused even when I click a button outside it 如果单击并按住一个按钮,如何使用angular一次又一次触发鼠标单击按钮? - how can I fire the mouse click button again and again using angular if I click and hold a button? 我怎样才能做到这一点当我点击一个按钮时它会解析一个 promise,如果我再次点击它会等到之前的 promise 被解析 - How can i make so when i click a button it will resolve a promise, and if i click again it will wait untill the previous promise is resolved 如何使.toggleClass按钮单击以使其在不同页面上保持效果? - How can I make a .toggleClass button click keep its effects on different pages? 如何在单击按钮时使div的高度更大,然后再次单击时又变回其原始高度? - How can I make the height of a div bigger onclick of a button and then change back to it's original height when click again? 如何单击同一按钮99次或直到代码结束? - How Can i Click on The Same Button For 99 Times or Till The Code Ends? 我如何在点击表格时更改 css 样式 - how can i change the css style on click with table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM