縹緲止盈
2019-07-03 15:43:24
聽“風(fēng)格改變”事件有可能嗎?是否可以在jQuery中創(chuàng)建可以綁定到任何樣式更改的事件偵聽器?例如,如果我想在元素更改維度時“做”某件事,或者在樣式屬性中做任何其他更改,我可以這樣做:$('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);
}})();這將臨時覆蓋內(nèi)部Prototype.css方法,并在最后用觸發(fā)器重新定義它。所以它是這樣工作的:$('p').bind('style', function(e) {
console.log( $(this).attr('style') );});$('p').width(100);$('p').css('color','red');
3 回答

梵蒂岡之花
TA貢獻(xiàn)1900條經(jīng)驗 獲得超5個贊
css

江戶川亂折騰
TA貢獻(xiàn)1851條經(jīng)驗 獲得超5個贊
var observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutationRecord) { console.log('style changed!'); }); });var target = document.getElementById('myId');observer.observe(target, { attributes : true, attributeFilter : ['style'] });

翻閱古今
TA貢獻(xiàn)1780條經(jīng)驗 獲得超5個贊
(function() { orig = $.fn.css; $.fn.css = function() { var ev = new $.Event('style'); orig.apply(this, arguments); $(this).trigger(ev); }})();
添加回答
舉報
0/150
提交
取消