contenteditable change events

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

    Using DOMCharacterDataModified under MutationEvents will lead to the same. The timeout is setup to prevent sending incorrect values (e.g. in Chrome I had some issues with space key)

    var timeoutID;
    $('[contenteditable]').bind('DOMCharacterDataModified', function() {
        clearTimeout(timeoutID);
        $that = $(this);
        timeoutID = setTimeout(function() {
            $that.trigger('change')
        }, 50)
    });
    $('[contentEditable]').bind('change', function() {
        console.log($(this).text());
    })
    

    JSFIDDLE example

提交回复
热议问题