【问题标题】:Capturing cursor position in javascript inline with element在与元素内联的javascript中捕获光标位置
【发布时间】:2011-02-11 15:25:11
【问题描述】:

当表格单元格悬停在鼠标上时,我正在尝试在鼠标旁边弹出一个 div。

<td onmouseover="bubblePopup("param1","param2");">This is the cell</td>

是否可以使用我的 bubblePopup 函数获取光标位置。

function bubblePopup(param1, param2){
    var newdiv = document.createElement('div');
    newdiv.setAttribute('id', param1);

    newdiv.style.width = "200px";
    newdiv.style.height = "80px";

    newdiv.style.position = "absolute";
    newdiv.style.left = cursorX + "px";
    newdiv.style.top = cursorY + "px";

    newdiv.innerHTML = "content";
    document.body.appendChild(newdiv);
}

我更喜欢原生 javascript(但也会考虑 jquery 选项)。它只需要在 Firefox 3.5 及更高版本中工作。

【问题讨论】:

    标签: javascript cursor-position


    【解决方案1】:

    我拼凑了一个可能会让你走上正轨的小提琴。

    http://www.jsfiddle.net/dduncan/WccJw/2/

    (稍微修饰一下)

    【讨论】:

    • 关于如何使销毁事件仅在光标不在表格单元格或新创建的 div 上方时发生的任何建议,以便我可以单击 div 中的链接?
    【解决方案2】:

    http://jsfiddle.net/CtCXE/

    var td = document.getElementById("thetd");
    
    td.onmouseover = function(e){bubblePopup(e, 'param1','param2')};
    
    function bubblePopup(e, param1, param2){
        var newdiv = document.createElement('div');
        newdiv.setAttribute('id', param1);
    
        newdiv.style.width = 200;
        newdiv.style.height = 80;
    
        var cursorX = e.pageX,
            cursorY = e.pageY;
    
        newdiv.style.position = "absolute";
        newdiv.style.left = cursorX + 'px';
        newdiv.style.top = cursorY + 'px';
    
        newdiv.innerHTML = "content";
        document.body.appendChild(newdiv);
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-19
      • 2023-03-10
      • 2013-07-08
      • 1970-01-01
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多