更改contenteditable时触发事件 [英] trigger an event when contenteditable is changed

查看:1417
本文介绍了更改contenteditable时触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更改divs值后,如何触发事件?

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

<div class="changeable" contenteditable="true"> Click this div to edit it <div>

因此,当其内容更改时,我想创建警报和/或执行其他操作:

So when its content changes I want to create an alert and/or do other things:

$('.changeable').text().change(function() {
  alert('Handler for .change() called.');
});

推荐答案

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

Just store the contents to a variable and check if it is different after blur() event. If it is different, store the new contents.

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

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

这篇关于更改contenteditable时触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆