How to change onmouseover event using javascript?

I have an HTML range that changes its onmouseover and onmouseout class:

<span id="someSpan" class="static" onmouseover="this.className='active'" onmouseout="this.className='static'">Some Text</span>

I want to be able to enable and disable onmouseover and onmouseout events using a Javascript function called elsewhere on the page. Is it possible?

+3
source share
5 answers

Of course.

document.getElementById('someSpan').onmouseover = 
     function() { 
         this.className='newactive'; 
     };
+2
source

A safe way is to use a Javascript toolkit like Prototype or JQuery.

All of them can do such things easily, but a cross browser works.

For example, here's how you do it in jQuery .

+1
source

, jQuery , JavaScript, , :

document.getElementById( "elementId" ) onMouseOut = FunctionName;.

, jQuery 20 ( ), . - , , JavaScript.

+1

, , onmouseover onmouseout Element , :

// Define your handler somewhere
function myNewHandler()
{
    // ...new logic...
}

// And where you want to swap it in:
document.getElementById('someSpan').onmouseover = myNewHandler;

... . addEventListener attachEvent ( , IE) . , Prototype, jQuery , .

+1

:

document.getElementById("someSpan").onmouseover = function()
{
    //New code
};//Do not forget the semicolon
0

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


All Articles