Jquery通过$(this).trigger('mousedown')从直接的mousedown事件和模拟中捕获不同的数据;

时间:2014-12-09 11:05:12

标签: javascript jquery triggers mouseevent

我需要在我的网页中捕获一些数据然后用户制作mousedown事件或在用户按页面元素上的TAB键时模拟它。 对于mousedown,我使用标准代码,如:

$('*').on('mousedown', function (e) {
    // make sure the event isn't bubbling
    if (e.target != this) {
        return;
    }
    //...my code
 });

并完成所有工作,对于TAB键压力,我使用此代码模拟mousedown事件

$(':input').keydown(function(e) {
  var keyCode = e.keyCode || e.which; 

  if (keyCode == 9) {
    $(this).trigger('mousedown');
  }
});

所有似乎都已完成,但随后我用

查看了e数据
console.dir(e)

存在许多差异,在第二种情况下有许多错过的数据:

相同元素点击和选项卡活动

点击 mousedown事件:

enter image description here

TAB 和$(this).trigger(' mousedown');

enter image description here

数据少得多!!例如,我需要e.pageX和e.pageY参数,但如果我触发事件则不存在。 我怎样才能在两种情况下获得相同的电子数据?

提前致谢

1 个答案:

答案 0 :(得分:1)

在你的第一次印刷中,你可以看到" OriginalEvent:MouseEvent"这是提供pageX / pageY的那个...当你模拟" mousedown"你没有原创活动。根据触发处理程序的事件类型,您无法访问原始事件。也许情况就是这样。