Saturday, June 1, 2024
 Popular · Latest · Hot · Upcoming
178
rated 0 times [  179] [ 1]  / answers: 1 / hits: 54842  / 9 Years ago, fri, may 8, 2015, 12:00:00

I have a div that has hover:



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


But I want to disable the hover when you click on the div.


More From » jquery

 Answers
57

Option 1. The Javascript solution



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



Alternatively, in pure CSS (although without rule persistence)





div {
color: blue;
}
div:not(:focus):hover {
color: red;
}
div:focus {
outline: none;
}

<div tabindex=0>hover!</div>




[#66682] Wednesday, May 6, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yaquelina

Total Points: 517
Total Questions: 101
Total Answers: 96

Location: Egypt
Member since Tue, Jul 6, 2021
3 Years ago
yaquelina questions
;