contenteditable change events

前端 未结 19 2355
粉色の甜心
粉色の甜心 2020-11-22 01:36

I want to run a function when a user edits the content of a div with contenteditable attribute. What\'s the equivalent of an onchange

19条回答
  •  自闭症患者
    2020-11-22 01:48

    non jQuery quick and dirty answer:

    function setChangeListener (div, listener) {
    
        div.addEventListener("blur", listener);
        div.addEventListener("keyup", listener);
        div.addEventListener("paste", listener);
        div.addEventListener("copy", listener);
        div.addEventListener("cut", listener);
        div.addEventListener("delete", listener);
        div.addEventListener("mouseup", listener);
    
    }
    
    var div = document.querySelector("someDiv");
    
    setChangeListener(div, function(event){
        console.log(event);
    });
    

提交回复
热议问题