【问题标题】:Getting x,y coordinates on click without passing the event input在不传递事件输入的情况下点击获取 x,y 坐标
【发布时间】:2012-09-16 23:43:49
【问题描述】:

好吧,我有一个问题:如何在不传递事件输入的情况下获得点击坐标?我想不出一种在 Firefox 中有效的方法,因为我想在使用修改后的 window.confirm 函数触发确认操作时获取坐标。示例:

window.confirm = function() {
    if ( arguments[0] )
    {
        text_confirm = arguments[0];
        x_pos = window.event.clientX;
        y_pos = window.event.clientY;
    }
}

这是在以下时间触发的:

<a onclick="if (confirm('Are you sure?')) document.location='....'; return false;" href="javascript:void(1);"></a>

此代码在 Chrome 中有效,但显然在 Firefox 中我必须使用 onClick="getCoords(event);" 之类的代码。而且我无法在 window.confirm 的覆盖范围内发送事件输入。

也赞赏另一种方法,但要知道这一点:

!!我无法更改 onClick 按钮上的操作,它必须只有 window.confirm()

【问题讨论】:

  • 你能绑定第二个处理程序并使用event 来检索 x,y 位置吗?如果是这样,您可以将其存储在某个地方并在您的确认功能中使用它。
  • 是的,你是对的。我可以这样做,但不建议这样做。如果没有找到其他解决方案,我可能会这样做。
  • Ty Shikyo,幸运的是我可以更改 onClick 事件,因为链接是使用 sprintf( DELETE, $url_delete ) 生成的,我可以在确认操作前面的 DELETE 常量中插入另一个函数来获取坐标。

标签: javascript jquery jquery-events overwrite confirm


【解决方案1】:

您可以简单地将坐标保存在变量中,然后在函数中使用它们:

var coords = getCoords(event); if (confirm('Are you sure?')) { // use stored coords and stuff ... }

如果您无法使用此节点访问 html,则可以在代码中重新定义侦听器:

$('#element').click(function(event) {
  if (!confirm('are you sure?'))
    return false;

  getCoords(event);
  // ...

  return false;
});

【讨论】:

  • 这对我不起作用,因为不可能将 id 附加到元素上,而且每页有很多项目需要处理。
猜你喜欢
  • 1970-01-01
  • 2014-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多