4

一点上下文:
我正在开发的应用程序有一个右键单击屏幕上某些对象的上下文菜单。当前的设计是这些对象中的每一个都侦听右键单击,发送 AJAX 请求以获取该对象的上下文数据,使用该数据从 Dojo 0.4.3(我知道!)创建一个 PopupMenu2,然后生成右键单击以启动 Dojo 菜单。

我试图找出一种为所有浏览器生成右键单击事件的方法。目前,我们只支持 IE 并使用 oncontextmenu 事件。

限制:

  • 没有 jQuery :(
  • 我无法为屏幕上的对象预加载所有数据来创建 Dojo 菜单并避免 AJAX 请求。
4

1 回答 1

4

这应该让您开始生成右键单击事件。右键的关键是button参数:button = 2。

if (document.createEvent) {
  var rightClick = document.createEvent('MouseEvents');
  rightClick.initMouseEvent(
    'click', // type
    true,    // canBubble
    true,    // cancelable
    window,  // view - set to the window object
    1,       // detail - # of mouse clicks
    10,       // screenX - the page X coordinate
    10,       // screenY - the page Y coordinate
    10,       // clientX - the window X coordinate
    10,       // clientY - the window Y coordinate
    false,   // ctrlKey
    false,   // altKey
    false,   // shiftKey
    false,   // metaKey
    2,       // button - 1 = left, 2 = right
    null     // relatedTarget
  );
  document.dispatchEvent(rightClick);
} else if (document.createEventObject) { // for IE
  var rightClick = document.createEventObject();
  rightClick.type = 'click';
  rightClick.cancelBubble = true;
  rightClick.detail = 1;
  rightClick.screenX = 10;
  rightClick.screenY = 10;
  rightClick.clientX = 10;
  rightClick.clientY = 10;
  rightClick.ctrlKey = false;
  rightClick.altKey = false;
  rightClick.shiftKey = false;
  rightClick.metaKey = false;
  rightClick.button = 2;
  document.fireEvent('onclick', rightClick);
}

我建议使用 Google 搜索“document.createEvent”和“document.createEventObject”,以获取有关 Mozilla 和 MSDN 站点的 API 的更多详细信息。

希望这可以帮助!

于 2009-10-16T21:56:42.647 回答