trigger an event when contenteditable is changed

前端 未结 12 806
抹茶落季
抹茶落季 2020-11-29 06:14

When a divs value is changed, how Can I trigger an event?

Click this div to edit it
12条回答
  •  时光取名叫无心
    2020-11-29 07:04

    Another solution which is slightly modified version of previous ones but it may be more comfortable to use for some of you.

    Idea is to save origin value and compare it with current value in 'blur' event. I save origin value as attribute of editable div tag (instead of creating variable for origin value). Using attribute as container for origin value is more comfortable when one has a huge number of editable divs in table (as cells). So it is easy to get origin value since you don't need to create many variables.

    See my complete example code:

    editable div

    originValue

    detecting changes in blur

    var cells = $("#table").find('td>div[contenteditable=true]');
    
    cells.each(function () {    
    
            $(this).blur(function() {
    
                        if ($(this).text() != $(this).attr("origin")) {
                            console.log('changed');
                        }
            });    
    });
    

提交回复
热议问题