Simulate Mouse Over in Vimperator plugin

荒凉一梦 提交于 2019-11-26 15:59:48

问题


I'm attempting to write a Vimperator plugin to allow use of hints mode to simulate mouse over on drop down menus. I have the hints mode working and can correctly choose elements that have mouseover events attached. The problem is my function to simulate the mouse over is not working. This is what I currently have:

function SimulateMouseOver(elem)
{
    var evt = elem.ownerDocument.createEvent('MouseEvents');
    evt.initMouseEvent('mouseover',true,true,
        elem.ownerDocument.defaultView,0,0,0,0,0,
        false,false,false,false,0,null);
    var canceled = !elem.dispatchEvent(evt);
    if(canceled)
        alert('Event Cancelled');
}

The above code works for some pages but not for others. For example it doesn't work on AccuWeather. Any ideas how to simulate a mouse over that will work for most pages?


回答1:


here's some code to start with to create the event, simpler and works for more browsers (if you don't need to specify exact mouse coordinates)

        if( document.createEvent ) {
            var evObj = document.createEvent('MouseEvents');
            evObj.initEvent( 'mouseover', true, false );
            elem.dispatchEvent(evObj);
        } else if( document.createEventObject ) {
            elem.fireEvent('onmouseover');
        }

hope that helps




回答2:


In case anyone bumps into this looking for a framework agnostic way to fire any HTML and Mouse event (and set some options, if needed), have a look here: How to simulate a mouse click using JavaScript?




回答3:


You may only trigger mouseover event on fields/elements that have a mouseover event bound to them. You can't just hijack the mouse.



来源:https://stackoverflow.com/questions/911586/simulate-mouse-over-in-vimperator-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!