1 /* 2 * jQuery input event 3 * Author: tangbin 4 * Blog: http://www.planeart.cn 5 * Date: 2011-08-18 15:15 6 */ 7 (function ($) { 8 9 // IE6\7\8不支持input事件,但支持propertychange事件 10 if ('onpropertychange' in document) { 11 // 檢查是否爲可輸入元素 12 var rinput = /^INPUT|TEXTAREA$/, 13 isInput = function (elem) { 14 return rinput.test(elem.nodeName); 15 }; 16 17 $.event.special.input = { 18 setup: function () { 19 var elem = this; 20 if (!isInput(elem)) return false; 21 22 $.data(elem, '@oldValue', elem.value); 23 $.event.add(elem, 'propertychange', function (event) { 24 // 元素屬性任何變化都會觸發propertychange事件 25 // 須要屏蔽掉非value的改變,以便接近標準的onput事件 26 if ($.data(this, '@oldValue') !== this.value) { 27 $.event.trigger('input', null, this); 28 }; 29 $.data(this, '@oldValue', this.value); 30 }); 31 }, 32 teardown: function () { 33 var elem = this; 34 if (!isInput(elem)) return false; 35 $.event.remove(elem, 'propertychange'); 36 $.removeData(elem, '@oldValue'); 37 } 38 }; 39 }; 40 41 // 聲明快捷方式:$(elem).input(function () {}); 42 $.fn.input = function (callback) { 43 return this.bind('input', callback); 44 }; 45 46 })(jQuery);