简体   繁体   English

禁用:单击鼠标悬停

[英]Disable :hover on click

I have a div that has hover: 我有一个悬停的div

.div {
  // css
}
.div:hover {
  // css
}

But I want to disable the hover when you click on the div . 但是我想在你点击div时禁用悬停。

Option 1. The Javascript solution 选项1. Javascript解决方案

Simply add a class prohibiting the application of hover styling on click: 只需添加一个禁止在点击时应用悬停样式的类:

 $('div').on('click', function() { // when you click the div $(this).addClass('no-hover'); // add the class 'no-hover' }); 
 div { color: blue; } div:not(.no-hover):hover { /* only apply hover styling when the div does not have the class 'no-hover' */ color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>hover!</div> 

Option 2. The CSS solution 选项2. CSS解决方案

Alternatively, in pure CSS (although without rule persistence) 或者,在纯CSS中(尽管没有规则持久性)

 div { color: blue; } div:not(:focus):hover { color: red; } div:focus { outline: none; } 
 <div tabindex="0">hover!</div> 

你需要的只是这个:

$('[your selector]').css({'pointer-events': 'none'})

Try like this: 试试这样:

$('#myid').on({
    mouseover: function(){
        $(this).css({background: '#F94'});
    },
     mouseleave: function(){
        $(this).css({background: '#DDD'});
    },
    click: function(){
        $(this).off('mouseleave');
    }
});

JSFIDDLE DEMO JSFIDDLE DEMO

Try this: 尝试这个:

JQUERY JQUERY

$(function() {                       //run when the DOM is ready
  $(".div").click(function() {  //use a class, since your ID gets mangled
    $(this).addClass("active");      //add the class to the clicked element
  });
});

CSS CSS

.div.active:hover {
cursor:default;
//same CSS as the DIV

}
.disabled {

  opacity: 0.5;
  pointer-events: none;

}

.disabled:hover *

  pointer-events: none;
  cursor: not-allowed;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM