A new event, called TextInput, was introduced in the "DOM3 level Event" specification. By specification, this event is triggered when a user enters a character in an editable region. The behavior of this textinput event to replace KeyPress is slightly different.
One of the differences is that any element that can get focus can trigger the Kepress event, but only the editable region can trigger the TextInput event.
The two extinput events are triggered only when the user presses a key that can enter the actual character, and the KeyPress event is triggered when the key that affects the text display is pressed (for example, backspace).
Because the TextInput event is primarily considered a character, its event object also contains a data property whose value is the user-entered character (not the character encoding). In other words, if the user presses the S key without pressing the file key, the Data property is "s", and if you press the key while holding down the file key, the value of data is "s".
Here is an example of using the TextInput event:
var eventutil = { Getevent:function (event) { Return event? Event:window.event; }, Addhandler:function (element, type, handler) { if (Element.addeventlistener) { Element.addeventlistener (type, handler, false); } else if (element.attachevent) { Element.attachevent ("On" + type, handler); } else { Element["on" + type] = handler; } } }; var textbox = document.getElementById ("MyText"); Eventutil.addhandler (textbox, "TextInput", function (event) { event = Eventutil.getevent (event); alert (event.data); }); Copy Code In this example, the characters inserted into the text box are displayed by a warning box.
Currently only SAFARI3 and Chrome browsers support TextInput events. As a result, this event may not be available in cross-browser development |