contenteditable change events

前端 未结 19 2341
粉色の甜心
粉色の甜心 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 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!');
        }
    });
    

提交回复
热议问题