jquery 获取被激发(触发)的事件的名称(类型)

8i9zcol2  于 2023-10-17  发布在  jQuery
关注(0)|答案(3)|浏览(127)

我有以下代码:

$('#button').on('click change', function() {
    alert('Who fired me, click or change?');
});

我如何知道调用的事件是“单击”还是“更改”?

g0czyy6m

g0czyy6m1#

event.type会让你得到你想要的。
DEMO
标签:List of event types

$('#button').on('click change', function(event){
    console.log(event.type + ' is fired');
});
vsnjm48y

vsnjm48y2#

@Vega解决方案对于简单事件是正确的。但是,如果你命名你的事件,(即,player.play),那么你也必须获得命名空间。例如,假设我触发了事件:

$('#myElement').trigger('player.play');

然后,要获取完整的事件名称,您需要执行以下操作:

$('#myElement').on('player.play', function(e) {
    console.log('Full event name: ' + e.type + '.' + e.namespace);
});
nue99wik

nue99wik3#

为了监听触发的事件,可以使用下面的代码段:

$(document).on("abort activate afterprint beforeactivate beforecopy beforecut beforedeactivate beforepaste beforeprint beforeunload blur bounce change CheckboxStateChange click contextmenu copy cut dblclick deactivate deactivate DOMAttrModified DOMCharacterDataModified DOMFocusIn DOMFocusOut DOMMouseScroll DOMNodeInserted DOMNodeInsertedIntoDocument DOMNodeRemoved DOMNodeRemovedFromDocument DOMSubtreeModified drag dragdrop dragend dragenter dragexit draggesture dragleave dragover dragstart drop error error (window) finish focus focusin focusout hashchange help input keydown keypress keyup load message mousedown mouseenter mouseleave mousemove mouseout mouseover mouseup mousewheel offline online overflow overflowchanged paste RadioStateChange readystatechange readystatechange (XMLDocument) readystatechange (XMLHttpRequest) reset resize scroll search select selectionchange selectstart start stop submit textInput underflow unload ",function(e){
      console.log(e.type);
    });

这是一个有点长,但肯定会有帮助。:)

相关问题