如果是文本框用onchange,oninput,onpropertychange都可以实时监控值发生变化,但是div设置了属性contenteditable(可编辑文档)就不管用了。
最简单的方法用oninput事件,可惜ie下支持度不好
addEvent(doc,'input',function(){ //do something... });
那么自己实现一个:
var oldValue = context.getSource(), newValue; ['blur','keyup','mouseup'].forEach(function(type){ addEvent(doc,type,function(){ newValue = context.getSource(); if(oldValue != newValue){ //do something... oldValue = newValue; } }); });
JQ实现:
(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); //调用:$('.wysiwyg').wysiwygEvt();