11
是否可以听“风格改变”事件?
是否可以在jQuery中创建可以绑定到任何样式更改的事件侦听器?例如,如果我想在元素更改尺寸或对style属性进行任何其他更改时“执行”操作,则可以执行以下操作: $('div').bind('style', function() { console.log($(this).css('height')); }); $('div').height(100); // yields '100' 这将非常有用。 有任何想法吗? 更新 很抱歉自己回答这个问题,但是我写了一个可能适合其他人的简洁解决方案: (function() { var ev = new $.Event('style'), orig = $.fn.css; $.fn.css = function() { $(this).trigger(ev); return orig.apply(this, arguments); } })(); 这将临时覆盖内部prototype.css方法,并在末尾使用触发器重新定义它。所以它是这样的: $('p').bind('style', function(e) { console.log( $(this).attr('style') ); }); $('p').width(100); $('p').css('color','red');
206
javascript
jquery