在JavaScript中执行默认操作后如何捕获事件 [英] How to catch event after default action was performed in JavaScript

查看:51
本文介绍了在JavaScript中执行默认操作后如何捕获事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了该文章(该文章)关于事件处理程序的以下概念:

I have read in the article (the article) following concepts about event handlers:

对于非冒泡事件,序列的调度是这样的:

For a non-bubbling event, the sequence of the dispatch is like this:

  1. 捕获阶段:所有捕获"在所有事件上触发事件处理程序祖先元素,从顶部开始下来.
  2. 在目标上触发了该事件元素,表示所有事件在元素上注册的处理程序针对特定事件执行(按未定义的顺序!)
  3. 已执行默认操作(如果它在任何一个都没有被取消处理程序)

对于冒泡事件,序列就像这样:

For a bubbling event, the sequence is like this:

  1. 捕获阶段:所有捕获"在所有事件上触发事件处理程序祖先元素,从顶部开始下来.
  2. 在目标上触发了该事件元素
  3. 冒泡阶段:已触发事件在所有祖先元素上目标向上.
  4. 已执行默认操作(如果它在任何一个都没有被取消处理程序)

此处,默认操作本质上是浏览器活动,用户在产生事件时会期望该活动,即,按下键时在文本区域中出现字符.

Here, default action is essentially a browser activity which user expects when produced an event, i.e. character appearance in textarea when key was pressed.

请问现在执行默认操作后,将如何调用附加的回调函数?所以我想赶上一个事件,当字符出现在文本区域中时.

Does any body now how attach callback that will be called after default action is performed? So I would like to catch an event when character appeared in textarea.

onchange不是解决方案,因为当焦点丢失时会触发它.onkeyup也不是解决方案

onchange is not a solution because it is fired when focus lost. onkeyup is not the solution also

有什么想法吗?

[UPD]我正在尝试在发生更改后立即捕获textarea值更改.这是用于将textarea的值复制到div.因此,当我使用onkeydown事件时,div内容的更新会延迟一键.我想在按下按键后马上拥有.

[UPD] I am trying to catch a textarea value change right after the change was happened. This is for copying value of textarea to a div. So when I use onkeydown event, the div content is updated with a delay of one key press. I want to have right after keypressed.

推荐答案

您需要onkeyup/onkeypress组合:

You need onkeyup/onkeypress combination:

<script type="text/javascript">
$(document).ready(function(){
    // single button (abcd)
    $("#source").keyup(function() {
        $("#target").html( $(this).val() );
    });
    // pressed button (aaaaaaaa)
    $("#source").keypress(function() {
        $("#target").html( $(this).val() );
    });
});
</script>
<textarea id="source"></textarea>
<div id="target"></div>

此示例使用jQuery.

This example uses jQuery.

这篇关于在JavaScript中执行默认操作后如何捕获事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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