如果是文本框用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();