使用谷歌图表将mousedown事件注册为click

Registering mousedown event as click with google charts

本文关键字:事件 注册 click mousedown 谷歌      更新时间:2023-09-26

尽管遇到了障碍,但我正试图将鼠标按下事件作为点击谷歌图表的方式发送,不确定为什么会出现以下错误。

我使用的是jQuery和谷歌图表api,visualisationOverlay是一个位于图表顶部的绝对div,错误是当dispatchEvent将修改后的事件发送到谷歌图表iframe时。

$('#visualizationOverlay').live('mousedown',function(e){
    e.type = "click";
    vis = document.getElementById($('#visualization').find('iframe').attr('id'));
    console.log(vis);
    vis.dispatchEvent(e);
});

我在firefox 中得到以下错误

NS_ERROR_ILLEGAL_VALUE: Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIDOMEventTarget.dispatchEvent]

如果有任何关于我哪里出错的提示,我们将不胜感激!

不能触发触发到另一个元素的事件。您需要创建一个新事件,或者只使用jQuerys trigger

看看这个例子。单击第一个元素会按预期将错误作为第二个日志。

$('#id1').on('mousedown', function(e){
  e.type = 'click';
  $('#id2').get(0).dispatchEvent(e);
});
$('#id2').on('mousedown', function(e){
  $('#id1').trigger('click')
});
$('#id1').on('mousedown', function(e){
  console.log('mousedown');
});
$('#id2').on('click', function(e){
  console.log('click');
});