gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:16:42 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com