contenteditable change events

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

    I built a jQuery plugin to do this.

    (function ($) {
        $.fn.wysiwygEvt = function () {
            return this.each(function () {
                var $this = $(this);
                var htmlold = $this.html();
                $this.bind('blur keyup paste copy cut mouseup', function () {
                    var htmlnew = $this.html();
                    if (htmlold !== htmlnew) {
                        $this.trigger('change')
                    }
                })
            })
        }
    })(jQuery);
    

    You can simply call $('.wysiwyg').wysiwygEvt();

    You can also remove / add events if you wish

提交回复
热议问题