Trigger onmouseover event programmatically in JavaScript

前端 未结 8 671
庸人自扰
庸人自扰 2020-11-27 18:58

Is there a way to programmatically trigger the onmouseover event in plain JavaScript? or \"extract\" the method from the onmouseover event to call

相关标签:
8条回答
  • For me following worked:

    ​document.getElementById('xyz').dispatchEvent(new MouseEvent('mouseover', { 'bubbles': true }));
    

    Also:

    ​document.getElementById('xyz').dispatchEvent(new MouseEvent('mouseover', { 'view': window, 'bubbles': true, 'cancelable': true }));
    
    0 讨论(0)
  • 2020-11-27 19:18
    const mouseoverEvent = new Event('mouseover');
    
    whateverElement.dispatchEvent(mouseoverEvent);
    
    0 讨论(0)
  • 2020-11-27 19:20

    I had to revise my RefreshMouseEvents set of functions after more testing. Here is the seemingly perfected version (again only IE9 tested):

    function RefreshMouseEvents( ElementId ) 
    {
        FireEvent( ElementId, 'mouseover' );
        setTimeout( "TriggerMouseEvent( '" + ElementId + "', '" + event.clientX + "', '" + event.clientY + "' )", 1 );
    }
    
    function TriggerMouseEvent( ElementId, MouseXPos, MouseYPos )
    {
        if( IsMouseOver( ElementId, (1*MouseXPos), (1*MouseYPos) ) )
            FireEvent( ElementId, 'mouseover' );
        else    
            FireEvent( ElementId, 'mouseout' );
    }
    
    function IsMouseOver( ElementId, MouseXPos, MouseYPos )
    {
        if( document.getElementById(ElementId) != null )    
        {
            var Element = document.getElementById(ElementId);   
            var Left  = Element.getBoundingClientRect().left, 
                Top   = Element.getBoundingClientRect().top,
                Right = Element.getBoundingClientRect().right,
                Bottom  = Element.getBoundingClientRect().bottom;       
            return ( (MouseXPos >= Left) && (MouseXPos <= Right) && (MouseYPos >= Top) && (MouseYPos <= Bottom))    
        }
        else
            return false;
    }
    
    function FireEvent( ElementId, EventName )
    {
        if( document.getElementById(ElementId) != null )    
        {   
            if( document.getElementById( ElementId ).fireEvent ) 
            {
                document.getElementById( ElementId ).fireEvent( 'on' + EventName );     
            }
            else 
            {   
                var evObj = document.createEvent( 'Events' );
                evObj.initEvent( EventName, true, false );
                document.getElementById( ElementId ).dispatchEvent( evObj );
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-27 19:28

    This worked for me in IE9 at least. Should be cross-browser compatible or close to it...

    function FireEvent( ElementId, EventName )
    {
        if( document.getElementById(ElementId) != null )    
        {   
            if( document.getElementById( ElementId ).fireEvent ) 
            {
                document.getElementById( ElementId ).fireEvent( 'on' + EventName );     
            }
            else 
            {   
                var evObj = document.createEvent( 'Events' );
                evObj.initEvent( EventName, true, false );
                document.getElementById( ElementId ).dispatchEvent( evObj );
            }
        }
    }
    

    For onmouseover example, call the function like this

    FireEvent( ElementId, "mouseover" );
    
    0 讨论(0)
  • 2020-11-27 19:29
    ​<a href="index.html" onmouseover="javascript:alert(0);" id="help"​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​>help</a>​​​​​​​​​​​​​​​​​​​​​​​​​​​
    
    ​document.getElementById('help').onmouseover();​​​​​​​
    
    0 讨论(0)
  • 2020-11-27 19:30

    Without going into too much detail, I had an img with rollovers, i.e. mouseout/overs that set the img src to hidden form values (or this could have done in a different context with gloabl variables). I used some javascript to swap both of my over/out image values and I called the called FireEvent( ElementId, "mouseover" ); to trigger the change. My javascript was hiding / displaying elements on the page. This caused the cursor to sometimes be over the img I used to trigger the event - which was the same as the one I was swapping out, and sometimes the cursor was not over the img after the click.

    Mouseover/out does not fire unless you exit and re-enter an element, so after my event was triggered the mouseover/out needed "retriggering" to account for the new cursor position. Here is my solution. After I hide / display various page elements, and to do my img src swapping as described, I call the function RefreshMouseEvents( ElementId ) instead of FireEvent( ElementId, "mouseover" ).

    This works in IE9 (not sure about other browsers).

    function RefreshMouseEvents( ElementId )
    {
        FireEvent( ElementId, 'mouseover' );
        setTimeout( "TriggerMouseEvent( '" + ElementId + "' )" , 1 );
    }
    
    function TriggerMouseEvent( ElementId )
    {
        if( IsMouseOver( ElementId, event.clientX, event.clientY ) )
            FireEvent( ElementId, 'mouseover' );
        else    
            FireEvent( ElementId, 'mouseout' );
    }
    
    function IsMouseOver( ElementId, MouseXPos, MouseYPos )
    {
        if( document.getElementById(ElementId) != null )    
        {
            var Element = document.getElementById(ElementId);   
            var Left  = Element.getBoundingClientRect().left, 
                Top   = Element.getBoundingClientRect().top,
                Right = Element.getBoundingClientRect().right,
                Bottom  = Element.getBoundingClientRect().bottom;       
            return ( (MouseXPos >= Left) && (MouseXPos <= Right) && (MouseYPos >= Top) && (MouseYPos <= Bottom))    
        }
        else
            return false;
    }
    
    function FireEvent( ElementId, EventName )
    {
        if( document.getElementById(ElementId) != null )    
        {   
            if( document.getElementById( ElementId ).fireEvent ) 
            {
                document.getElementById( ElementId ).fireEvent( 'on' + EventName );     
            }
            else 
            {   
                var evObj = document.createEvent( 'Events' );
                evObj.initEvent( EventName, true, false );
                document.getElementById( ElementId ).dispatchEvent( evObj );
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题