keyup 没有触发

keyup is not firing

我有这个工作,但它已经停止了。奇怪的是 'mouseup' 有效,但 'keyup' 无效。

$(文档).ready(函数(){

 $('#chordprodiv').on('keyup mouseup', 'textarea', function () {
    chordpro_toCHORDSHEET();
});

});

评论后有 2 个修复。我更新了 jquery 并将函数更改为 .on,并更正了事件格式,使它们位于同一字符串中。该代码已经在 document.ready 中,但为了清楚起见,我添加了它。

从那以后,我已将父 div 作为目标,并根据另一项建议确定了 'textarea' 选择器。

还有html

 <textarea id="chordpro" name="chordpro"></textarea>

.live 在 jQuery 的较新版本中已弃用,您应该使用 .on

此外,您的事件应该在一个字符串中...

$('#chordpro').on('keyup mouseup', function () {
    chordpro_toCHORDSHEET();
});

事件处理器的调用格式如下。 $( 文档 ).on( 事件、选择器、数据、处理程序 );

问题在于您如何设置事件处理程序。 Jquery .on() 具有您正在使用的重载:

.on( events [, selector ] [, data ], handler )

但是从文档(在 http://api.jquery.com/on/ 找到):

When a selector is provided, the event handler is referred to as delegated. The handler is not called when the event occurs directly on the bound element, but only for descendants (inner elements) that match the selector. jQuery bubbles the event from the event target up to the element where the handler is attached (i.e., innermost to outermost element) and runs the handler for any elements along that path matching the selector.

意思是你有两个选择。

1. 使用 .on

的另一个重载
$('#chordpro').on('keyup mouseup', function () {
...

2. 将事件处理程序放在父元素上

HTML:

<div id="chordpro-div">
  <textarea id="chordpro" name="chordpro"></textarea>
</div>

Javascript:

$('#chordpro-div').on('keyup mouseup', 'textarea', function () {
...