Receive mousemove events from iframe, too

前端 未结 6 1180
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 08:17

I have a javascript app, whichs adds a mousemove listener to the document. Problem: When the mouse is moved over an iframe, the function is NOT called.

Is there a wa

6条回答
  •  甜味超标
    2020-12-03 08:52

    I was having the same problem just now and I came up with this:

    $("iframe").contents().find("body").mousemove(function(cursor){
            $("#console").html(cursor.pageX+":"+cursor.pageY);
        });
        $(document).mousemove(function(cursor){
            $("#console").html(cursor.pageX+":"+cursor.pageY);
        });
    

    .contents().find("body") grabs the contents inside the iframe and .mousemove() can be used to add an event listener

    Test html...

提交回复
热议问题