2011-02-28 70 views
1

Icefaces有一个定位弹出窗口的方法,您在哪里点击鼠标。我禁用了鼠标单击坐标所在的代码部分,因为我想将此菜单选项放在我的targComp(实际上是div)所在的位置(因此固定位置为iso鼠标位置)。javascript - 位置函数返回0

调用的JavaScript方法是:

function contextMenuPopup(event, popupMenu, targComp) { 
var dynamic = $(popupMenu + "_dynamic"); 
if (!event) { 
    event = window.event; 
} 
if (event) { 
    event.returnValue = false; 
    event.cancelBubble = true; 

    if (event.stopPropagation) { 
     event.stopPropagation(); 
    } 

    var posx = 0; // Mouse position relative to 
    var posy = 0; // the document 
    /* 
    * if (event.pageX || event.pageY) { posx = event.pageX; posy = 
    * event.pageY; } else if (event.clientX || event.clientY) { posx = 
    * event.clientX + document.body.scrollLeft + 
    * document.documentElement.scrollLeft; posy = event.clientY + 
    * document.body.scrollTop + document.documentElement.scrollTop; } 
    */ 
    alert(Left(targComp)); 
    Ice.Menu.showIt(posX, posY, popupMenu, targComp); 
} 
} 

你看,我只评论旧代码,并添加一个警报,以找出是否我的方法,返回的targComp的位置是否正确计算值。

function Left(el) { 
var _x = 0; 
while(el && !isNaN(el.offsetLeft) && !isNaN(el.offsetTop)) { 
    _x += el.offsetLeft - el.scrollLeft; 
    el = el.parentNode; 
} 
return _x; 
} 

function Top(el) { 
    var _y = 0; 
    while(el && !isNaN(el.offsetLeft) && !isNaN(el.offsetTop)) { 
     _y += el.offsetTop - el.scrollTop; 
     el = el.parentNode; 
    } 
    return _y; 
} 

我不明白为什么我的警报返回0时,我肯定知道我targComp DIV是不是留下坐标......

你看不出什么问题? (是的,我知道我必须在showIt方法中替换posX和posY,但是在确定Left和Top是正确的之后,我会这样做的(顺便说一下,从here复制过来,所以已经确认这些方法是做工精细...)

那么它就是问题所在

HTML代码:

<div class="icePnlGrp graMainMenuTabDefault" id="frmMainMenu:divMenuPopupAP" onmouseover="contextMenuPopup(event, 'frmMainMenu:menuPopupAP_sub', 'frmMainMenu:divMenuPopupAPSmall');return false;"> 
    <label class="iceOutLbl graMainMenuTabText" id="frmMainMenu:j_id54">Application Portfolio</label> 
    <div class="icePnlGrp" id="frmMainMenu:divMenuPopupAPSmall" style="border-style:solid; border-width:1px;"> 
</div> 
</div> 

更新(解决上述问题后):我附上截图不知道为什么鼠标位置正确计算时,我按下该targetComp div中的点击但th的位置e div是错的...?

enter image description here

更新解决:看来,我确实需要targCompObject.offsetLeft,targCompObject.offsetTop, 代替calliny的那些顶部和左侧的功能。

其中

targCompObject = document.getElementById(targComp); 

所以最后通话:

Ice.Menu.showIt(targCompObject.offsetLeft, targCompObject.offsetTop, 
      popupMenu, targComp); 
+0

你确定'targComp'是你的div的有效引用吗?可能你没有传入有效的DOM元素作为参数 – davin 2011-02-28 13:14:16

+0

刚刚添加了html代码来证明... – 2011-02-28 13:22:35

+1

@Christian什么是'frmMainMenu:menuPopupAP_sub'相对于'frmMainMenu:divMenuPopupAP'?我认为您不需要将这些ID值硬编码到onmouseover属性中。 – 2011-02-28 13:34:15

回答

1

你传递的目标元素,而不是元素本身的ID

某处,在弹出的处理程序,你需要

targComp = document.getElementById(targComp); 

你可以把它先检查,看看它是否是一个字符串,这样你可以任选一个DOM元素引用调用它。

+0

呃...非常感谢........ – 2011-02-28 13:30:17

+0

你能否也请看看我的更新? – 2011-02-28 14:05:41

+0

好吧,你写信,现在解决了 - 所以一切都好? – Pointy 2011-02-28 14:24:00