javascript:可能添加在默认行为之后运行的事件处理程序? [英] javascript: Possible to add event handler that runs after the default behavior?

查看:93
本文介绍了javascript:可能添加在默认行为之后运行的事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是可能吗?

$ b我有一个事件处理程序,我希望在该事件的默认JavaScript处理发生后执行。
$ b

编辑:
例如:
每当一个keypress事件被触发,我正在记录关于该按键的事件信息(类似于console.log)。现在,当我按退格键并通过事件处理程序查看字段内容时,文本字段的值产生没有退格的值,因为事件处理程序在实际删除最后一个字母之前抓取该字段的内容。



在这种情况下,我希望发生默认的退格事件,然后在完成后触发我的事件处理程序。



谢谢!

解决方案

我相信你的问题是由于听到按键事件而不是 keyup 事件。基本上这意味着在字符放在dom中之前,您将获得textarea的值。



尝试将按键处理程序更改为键控处理程序。



一个例子是:

  $('input,textarea')。 (function(ev){

console.log($(this).val());

});

编辑:找到这个问题似乎是你的确切问题。


I have an event handler that I would like to have executed after the default javascript handling of that event occurs.

Is this possible?

EDIT: For example: everytime a keypress event is fired, i'm logging event information about that keypress(similar to console.log). Now, when I press backspace and view the fields contents via the event handler, the value of the text field produces the value without the backspace since the event handler grabs the contents of the field before the actual removal of the last letter.

I would like, in this case, for the default backspace event to occur and then have my event handler fired upon completion.

Thanks!

解决方案

I believe your problem is caused by listening for the keypress event instead of the keyup event. Basically this means that you are getting the value of the textarea before the character is put in the dom.

Try changing your keypress handler to a keyup handler.

An example would be:

$('input, textarea').keyup(function(ev) {

    console.log( $(this).val() );

});

Edit: Found this question which appears to be your exact problem.

这篇关于javascript:可能添加在默认行为之后运行的事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆