contenteditable change events

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

    The onchange event doesn't fires when an element with the contentEditable attribute is changed, a suggested approach could be to add a button, to "save" the edition.

    Check this plugin which handles the issue in that way:

    • Creating a quick and dirty jQuery contentEditable Plugin
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-22 01:50

    Here is the solution I ended up using and works fabulously. I use $(this).text() instead because I am just using a one line div that is content editable. But you may also use .html() this way you dont have to worry about the scope of a global/non-global variable and the before is actually attached to the editor div.

    $('body').delegate('#editor', 'focus', function(){
        $(this).data('before', $(this).html());
    });
    $('#client_tasks').delegate('.task_text', 'blur', function(){
        if($(this).data('before') != $(this).html()){
            /* do your stuff here - like ajax save */
            alert('I promise, I have changed!');
        }
    });
    
    0 讨论(0)
  • 2020-11-22 01:55

    This thread was very helpful while I was investigating the subject.

    I've modified some of the code available here into a jQuery plugin so it is in a re-usable form, primarily to satisfy my needs but others may appreciate a simpler interface to jumpstart using contenteditable tags.

    https://gist.github.com/3410122

    Update:

    Due to its increasing popularity the plugin has been adopted by Makesites.org

    Development will continue from here:

    https://github.com/makesites/jquery-contenteditable

    0 讨论(0)
  • 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')});
    
    0 讨论(0)
  • 2020-11-22 01:59

    Non JQuery answer...

    function makeEditable(elem){
        elem.setAttribute('contenteditable', 'true');
        elem.addEventListener('blur', function (evt) {
            elem.removeAttribute('contenteditable');
            elem.removeEventListener('blur', evt.target);
        });
        elem.focus();
    }
    

    To use it, call on (say) a header element with id="myHeader"

    makeEditable(document.getElementById('myHeader'))
    

    That element will now be editable by the user until it loses focus.

    0 讨论(0)
提交回复
热议问题