When you enter data in a text box, you can use onkeyup to detect events when the keyboard is pressed and released, Onpropertychange can capture each change, and onchange need to execute an event to catch it. But sometimes we enter data in the form of a paste rather than a keyboard input, which requires real-time detection of changes in the state of the text box.
Onpropertychange is not supported by Firefox, if you want to use the normal Firefox, you need to use Oninput properties, and need to register events with AddEventListener.
<div id= "msg" ></div> <input id= ' txt ' value= ' "/> <script>//functions performed when state changes handle () { document.getElementById (' msg '). innerhtml= ' The length of the text entered is: ' +document.getelementbyid (' txt '). value.length; //firefox detection state changes can only be used oninput, and need to use AddEventListener to register events. if (/msie/i.test (navigator.useragent))//ie Browser {document.getElementById (' txt '). Onpropertychange=handle} else {//non IE browsers, such as Firefox document.getElementById (' txt '). AddEventListener ("input", handle,false); } </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Example:
Copy Code code as follows:
<body>
<div>oninput Test </div>
<div id= "Testdiv" ><input id= ' tx1 ' name= ' tx1 ' value= ' "/></div>"
</body>
<script language= "JavaScript" >
<!--
function Getos () {//Determine browser type
var osobject = "";
if (Navigator.userAgent.indexOf ("MSIE") >0) {
return "MSIE";
}
if (Isfirefox=navigator.useragent.indexof ("Firefox") >0) {
return "Firefox";
}
if (Issafari=navigator.useragent.indexof ("Safari") >0) {
return "Safari";
}
if (Iscamino=navigator.useragent.indexof ("Camino") >0) {
return "Camino";
}
if (Ismozilla=navigator.useragent.indexof ("gecko/") >0) {
return "Gecko";
}
}
if (Navigator.userAgent.indexOf ("MSIE") >0) {
document.getElementById (' tx1 '). attachevent ("Onpropertychange", Txchange);
}else if (navigator.userAgent.indexOf ("Firefox") >0) {
document.getElementById (' tx1 '). AddEventListener ("input", txchange2,false);
}
function Txchange () {
Alert ("TestIE");
}
function TxChange2 () {
Alert ("Testfirefox");
}
</script>
The above is the compatible Firefox Onpropertychange event method.