contenteditable change events

前端 未结 19 2314
粉色の甜心
粉色の甜心 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 02:00

    A simple answer in JQuery, I just created this code and thought it will be helpful for others too

        var cont;
    
        $("div [contenteditable=true]").focus(function() {
            cont=$(this).html();
        });
    
        $("div [contenteditable=true]").blur(function() {
            if ($(this).html()!=cont) {
               //Here you can write the code to run when the content change
            }           
        });
    

提交回复
热议问题