当contenteditable发生变化时触发事件

joh*_*sly 42 jquery contenteditable

更改div值时,如何触发事件?

<div class="changeable" contenteditable="true"> Click this div to edit it <div>
Run Code Online (Sandbox Code Playgroud)

因此,当内容发生变化时,我想创建一个警报和/或做其他事情:

$('.changeable').text().change(function() {
  alert('Handler for .change() called.');
});
Run Code Online (Sandbox Code Playgroud)

Nik*_*las 45

只需将内容存储到变量中,并在blur()事件发生后检查它是否不同.如果不同,请存储新内容.

var contents = $('.changeable').html();
$('.changeable').blur(function() {
    if (contents!=$(this).html()){
        alert('Handler for .change() called.');
        contents = $(this).html();
    }
});
Run Code Online (Sandbox Code Playgroud)

示例:http://jsfiddle.net/niklasvh/a4QNB/

  • 这是一个肮脏的黑客,不应该使用。 (2认同)

Bru*_*eur 17

你可以简单地使用jQuery的data()函数的focus/blur事件:

// Find all editable content.
$('[contenteditable=true]')
    // When you click on item, record into data("initialText") content of this item.
    .focus(function() {
        $(this).data("initialText", $(this).html());
    });
    // When you leave an item...
    .blur(function() {
        // ...if content is different...
        if ($(this).data("initialText") !== $(this).html()) {
            // ... do something.
            console.log('New data when content change.');
            console.log($(this).html());
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

更新:使用Vanilla JS

// Find all editable content.
var contents = document.querySelectorAll("[contenteditable=true]");
[].forEach.call(contents, function (content) {
    // When you click on item, record into `data-initial-text` content of this item.
    content.addEventListener("focus", function () {
        content.setAttribute("data-initial-text", content.innerHTML);
    });
    // When you leave an item...
    content.addEventListener("blur", function () {
        // ...if content is different...
        if (content.getAttribute("data-initial-text") !== content.innerHTML) {
            // ... do something.
            console.log("New data when content change.");
            console.log(content.innerHTML);
        }
    });
});
Run Code Online (Sandbox Code Playgroud)


Blo*_*sie 12

我构建了一个jQuery插件来执行此操作.

(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);
Run Code Online (Sandbox Code Playgroud)

你可以简单地打电话 $('.wysiwyg').wysiwygEvt();

如果您愿意,也可以删除/添加活动


小智 11

目前最好的解决方案是HTML5输入事件

<div contenteditable="true" id="content"></div>

在你的jquery.

$('#content').on('input', (e) => {
    // your code here
    alert('changed')
});
Run Code Online (Sandbox Code Playgroud)

  • 唯一的问题是每次按键时都会触发“input”,而输入所有输入后最后会触发“change” - 对于我的情况,我需要一个更改事件而不是输入 (3认同)
  • 这应该是公认的答案——我认为它是最直接也是最好的答案。 (2认同)

小智 7

使用EventListener(不是jQuery方法)来做它更简单:

document.getElementById("editor").addEventListener("input", function() {
   alert("input event fired");
}, false);

  • 这对于最近的Mozilla和WebKit浏览器很有用,但遗憾的是在IE或Opera的任何版本中都不支持`contenteditable`. (3认同)

小智 6

这是我的方法......

$('.changeable').focusout(function() {
  alert('Handler for .change() called.');
});
Run Code Online (Sandbox Code Playgroud)