0

我在 IFRAME 中有一个 IFRAME,而我的 getMousePosition 方法无法检索 iframe 中的当前鼠标位置。它在第一个 Iframe 中工作,但是当我从父文档中的函数调用 getMousePosition 时,它返回后备值 600 和 350。仅供参考:我无法控制生成 IFrame 的内容,但它不是跨域访问。IFRAMES 和父文档都托管在同一台服务器上。我只是为 Internet Explorer 8 编程。所以浏览器兼容性不是问题。

  function getMousePosition(){
  if(!inframe)
      $(document).mousemove(function(e){
           mouseX = e.pageX 
           mouseY = e.pageY 
      });
  else
  {
    mouseX = 600;
    mouseY = 350;
  }

  //This is where I get the Iframe Document (I then parse through the document, picking up the specific links and storing them in the array verifiedlinks)
  var src = window.frames[iframeindex].document.getElementsByTagName("a");
  // This is where I call my function which uses the values returned by getMousePosition (verifiedlinks is an array of links inside the iframe):

    verifiedlinks[i].onmouseover = function() 
    {
        showPeopleDetails(this.getAttribute('username'));
    }
  // This should display User Details at the current Mousecoordinates
function showPeopleDetails(UserId){
var vpd = document.getElementById("PeopleDetails");
    if ( vpd != null ) {
        getMousePosition();
        vpd.style.left=mouseX+10; //mouseX and mouseY are defined globally
        vpd.style.top=mouseY+10;
        vpd.style.display="block";
    }
}

我已经阅读了这个问题:已解决的问题,但答案并没有解决我的问题。我发现了这个问题,但似乎没有一个答案对我有用。我新编辑的代码:

function showPeopleDetails(UserId, x, y){
var vpd = document.getElementById("PeopleDetails");
try
{
    if ( vpd != null ) {
        //getMousePosition();
        //alert("MouseX: " +mouseX+" MouseY: "+mouseY);
        //vpd.style.left=mouseX+10;
        //vpd.style.top=mouseY+10;
        vpd.style.left = x +10 - window.frames[2].document.body.scrollLeft;
        vpd.style.top = y +10 - window.frames[2].document.body.scrollTop;
     }
}
4

1 回答 1

1

如果您从父窗口调用 getMousePosition,则 document 将指向父窗口文档。您应该在 iframe 的上下文中调用此方法。此外,inframe 是在哪里定义的,您是否会在任何事件上更新其值?

您可以使用 jquery 将鼠标悬停事件附加到链接。使用它,您将获得事件对象,该对象提供链接相对于文档的鼠标 x/y 坐标。我希望它会帮助你。

$(verifiedlinks[i]).mouseover(function(e){
        showPeopleDetails($(this).attr('username'), e.pageX, e.pageY);
}

function showPeopleDetailsNow(UserId, x, y){
var vpd = document.getElementById("PeopleDetails");
    if ( vpd != null ) {
        getMousePosition();
        //vpd.style.left=mouseX+10; //mouseX and mouseY are defined globally
        //vpd.style.top=mouseY+10;
        vpd.style.left= x +10 + $(document).scrollTop(); //mouseX and mouseY are defined globally
        vpd.style.top= y +10;
        vpd.style.display="block";
    }
}
于 2011-08-01T14:13:13.353 回答