Temporary Onmouseover

The Css "hover" selector applies a temporary style to the element, it is not final:

div:hover {
 background-color: red;
}

I can do the same with javascript, but it is a bit complicated and impossible for a few elements:

var elem = document.getElementsByTagName ("div")[0];

elem.onmouseover = function () {
 this.style.backgroundColor = "red";
}

elem.onmouseout = function () {
 this.style.backgroundColor = "transparent";
}

Is there a better way? Something like that:

document.getElementsByTagName ("div")[0].ontemporarymouseover = function () { // LoL
 this.style.backgroundColor = "red";
}

thank

+3
source share
4 answers

// jQuery "Temporary mouse events"

$("element").bind
({
    mouseover:
        function ()
        {
        },
    mouseout:
        function ()
        {
        }
});

$("element").unbind('mouseover mouseout');

Hope this is a good approach to what you need.

0
source

No, there is no way to apply styles that go away on their own.

CSS , , onmouseover onmouseout. , class :hover, CSS. , :hover , CSS .

+2

JavaScript mouseover mouseout DOM, . CSS, .

+1

, jQuery JavaScript framework, :

$('div:first').hover(function(){
   $(this).css('background-color','red');
},function(){
   $(this).css('background-color','white');
});
0

Source: https://habr.com/ru/post/1745656/


All Articles