javascript - JavaScript 中的 keypress 和 change 有什么区别

标签 javascript jquery

我是 java 脚本的新手。我对按键和更改事件有点困惑。

示例代码如下

 $("input").live('change',function ()
    {
       Alert("Change event call");
    });

    /
    $("input").live('keypress',function ()
    {            
       Alert("Keypress event call");
    });

Keypress事件到底是什么时候调用和change事件调用的?

最佳答案

改变事件

The change event is sent to an element when its value changes. This event is limited to <input> elements, <textarea> boxes and <select> elements. For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus.

按键事件

The keypress event is sent to an element when the browser registers keyboard input. This is similar to the keydown event, except in the case of key repeats. If the user presses and holds a key, a keydown event is triggered once, but separate keypress events are triggered for each inserted character. In addition, modifier keys (such as Shift) trigger keydown events but not keypress events.

A keypress event handler can be attached to any element, but the event is only sent to the element that has the focus. Focusable elements can vary between browsers, but form elements can always get focus so are reasonable candidates for this event type.

引用资料:

关于javascript - JavaScript 中的 keypress 和 change 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13525958/

相关文章:

javascript - Jekyll Minimal Mistakes 主题 - 添加插件

javascript - 当我将鼠标悬停在 wordpress 循环中的每个帖子上时的不同操作

jquery - 无法通过Ajax请求获取CSV文本

javascript - Chrome 扩展程序可分析特定 div 中数字的页面源代码

javascript - javascript以表单形式注入(inject)密码安全吗?

javascript - 如何从按钮检查输入单选 - AngularJS

javascript - 为 Restful Web 服务构建 JSON 配置的正确方法

jquery 在字符串中使用变量作为appendTo

javascript - 刷新页面添加新的随机参数

javascript - 是否可以在 Google Apps 脚本中迭代 ENUM?