contenteditable change events

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

    Here is a more efficient version which uses on for all contenteditables. It's based off the top answers here.

    $('body').on('focus', '[contenteditable]', function() {
        const $this = $(this);
        $this.data('before', $this.html());
    }).on('blur keyup paste input', '[contenteditable]', function() {
        const $this = $(this);
        if ($this.data('before') !== $this.html()) {
            $this.data('before', $this.html());
            $this.trigger('change');
        }
    });
    

    The project is here: https://github.com/balupton/html5edit

提交回复
热议问题