How to tell whether input is entering or deleting?

how to tell whether input is being entered or deleted

Jul.17,2022

1, to judge the change, input is an increase in the number of words, deletion is a decrease in the number of words. The bidirectional binding of
2 and defineProperty is actually used to judge the number of words, but this lower version of IE does not support


    .
  1. listens for change events and then determines whether the length of the value increases or decreases.
  2. listens for the keypress event and then determines whether to press the delete key.

<body >
    <input type="text">
    <div></div>
<script>
    let dom = document.getElementsByTagName('input')[0];
    let value = dom.value;
    dom.oninput=()=>{
        let new_value = dom.value;
        if(value.length<new_value.length){
            document.getElementsByTagName('div')[0].innerHTML = "";
        }else{
            document.getElementsByTagName('div')[0].innerHTML = "";
        }
        value = new_value;
    }
</script>
</body>
Menu