如何在jQuery中将事件从一个函数传递到另一个函数

时间:2022-08-07 23:56:00



I need to pass somehow the information about pressed Ctrl key on invoking the event by jQuery click function. I want invoke this with ctrl key pressed.

我需要通过jQuery单击功能以某种方式传递有关按下Ctrl键的信息。我想按下ctrl键调用它。

$(selector).click();

This is simplified example:

这是一个简化的例子:

https://jsfiddle.net/62mdur6o/

When you click on the first cell of table ("One") you do not get information about present ctrl key in the event.
Is it possible to invoke click listener differently or to attach somehow this information to the event which will be passed to other listeners?

当您单击表格的第一个单元格(“One”)时,您无法获得有关事件中当前ctrl键的信息。是否可以以不同方式调用单击侦听器或以某种方式将此信息附加到将传递给其他侦听器的事件?

2 个解决方案

#1


1  

You can trigger event using:

您可以使用以下方式触发事

$('#cell').click(function (event) {
  event.stopPropagation();
  var e = jQuery.Event( event, {ctrlKey: event.ctrlKey} );
  $(this).next().trigger(e);
});

-demo-

#2


1  

Target each cells

针对每个细胞

    $('.cells').click(function (event) {
        if (event.ctrlKey) {
           alert('ctrl pressed');
        } else {
            alert('just clicked');
        }
     });

updated fiddle

#1


1  

You can trigger event using:

您可以使用以下方式触发事

$('#cell').click(function (event) {
  event.stopPropagation();
  var e = jQuery.Event( event, {ctrlKey: event.ctrlKey} );
  $(this).next().trigger(e);
});

-demo-

#2


1  

Target each cells

针对每个细胞

    $('.cells').click(function (event) {
        if (event.ctrlKey) {
           alert('ctrl pressed');
        } else {
            alert('just clicked');
        }
     });

updated fiddle