contenteditable change events

前端 未结 19 2319
粉色の甜心
粉色の甜心 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:56

    I have modified lawwantsin 's answer like so and this works for me. I use the keyup event instead of keypress which works great.

    $('#editor').on('focus', function() {
      before = $(this).html();
    }).on('blur keyup paste', function() { 
      if (before != $(this).html()) { $(this).trigger('change'); }
    });
    
    $('#editor').on('change', function() {alert('changed')});
    

提交回复
热议问题